Created
June 30, 2011 15:44
-
-
Save douglasmiranda/1056506 to your computer and use it in GitHub Desktop.
PaseInt PHP equivalent to the Javascript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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