<?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}?>Once I had the loop set up I simply added anis_ numeric()
check and inserted abreak
command the first time the check returned false – here is my use case:<?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?>
Tuesday, 12 May 2015
Loop through each character in a string in PHP
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment