Looping Through a String
Posted: Wed Jun 09, 2004 3:17 pm
How is this done in PHP? For example, I have the string "I can read", if I want to search through each member of the string, how do I go about this?
Thanks.
Thanks.
A community of PHP developers offering assistance, advice, discussion, and friendship.
http://forums.devnetwork.net/
Code: Select all
$string = "I can read";
$len = strlen($string);
for($x = 0; $x < $len; $x++)
{
echo $string{$x}."<br />\n";
}Code: Select all
switch($name{0})Code: Select all
<?php
$string = "Hello";
// for the first one
echo $string{0};
// for the second one
switch($string{0})
{
case 'H':
echo 'H found';
break;
default:
echo 'uncaught: '.$string{0}.'('.ord($string{0}).')'."\n";
break;
}
// alternate to second one
switch(ord($string{0}))
{
case ord('H'):
echo 'H found';
break;
default:
echo 'uncaught: '.$string{0}.'('.ord($string{0}).')'."\n";
break;
}
?>