Skip to content

Instantly share code, notes, and snippets.

@douglasmiranda
Created June 30, 2011 15:44
Show Gist options
  • Save douglasmiranda/1056506 to your computer and use it in GitHub Desktop.
Save douglasmiranda/1056506 to your computer and use it in GitHub Desktop.
PaseInt PHP equivalent to the Javascript
<?php
/**
* Just a parse int function, like the parseInt(string) from Javascript
*/
// If your PHP version is < 5.2, this is the solution:
function parse_int($string) {
$size_of_string = strlen($string);
for ($i = 0; $i < $size_of_string; $i++) {
if (is_numeric($string[$i]))
$numbers[$i] = $string[$i];
}
return join("", $numbers);
}
echo parse_int("a1b2c3ção");
// But if your PHP version is > 5.2 filter_var is enabled by default
// So, validations, parsers and more are available if you using filter_var
echo filter_var("a1b2c3ção",FILTER_SANITIZE_NUMBER_INT)
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment