Created
October 16, 2014 12:23
-
-
Save hlashbrooke/ee208fb8be43d23da5a9 to your computer and use it in GitHub Desktop.
PHP: Loop through each character in a string
This file contains hidden or 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 | |
$str = "String to loop through" | |
$strlen = strlen( $str ); | |
for( $i = 0; $i <= $strlen; $i++ ) { | |
$char = substr( $str, $i, 1 ); | |
// $char contains the current character, so do your processing here | |
} | |
?> |
This file contains hidden or 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 | |
$str = "123?param=value" | |
$strlen = strlen( $str ); | |
$id = ""; | |
for( $i = 0; $i <= $strlen; $i++ ) { | |
$char = substr( $str, $i, 1 ); | |
if( ! is_numeric( $char ) ) { break; } | |
$id .= $char; | |
} | |
// $id now contains the ID I need, in this case: 123 | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
There are always like 20-50 solutions to a problem. This 1 is cool ^_^