Hi,
I created a while-loop which will basically go through each number in a string and list the positions of the desired number.
For example suppose I have a string sa "12123124" and I want to list all the positions of "1" the output should be 0, 2 and 5.
My work out below is:
[code=php]
<html>
<body>
<?php
$number = "12123124";
$posFirst = 0; // Computer counts from 0.
$posLast = strlen($number)-1; // Since we are counting from 0, we need to
// adjust the last position accordingly
While($posFirst <= $posLast)
{
$posFirst = strpos($number, "1", $posFirst);
echo $posFirst."<p>" ;
$posFirst = $posFirst+1;
}
?>
</body>
</html>
[/code]
The output I get is an infinite loop and I don't get what's causing it to loop forever.
0
2
5
2
5
2
5
2
5
2
5
2
Any idea would be appreciated. Thanks.
I created a while-loop which will basically go through each number in a string and list the positions of the desired number.
For example suppose I have a string sa "12123124" and I want to list all the positions of "1" the output should be 0, 2 and 5.
My work out below is:
[code=php]
<html>
<body>
<?php
$number = "12123124";
$posFirst = 0; // Computer counts from 0.
$posLast = strlen($number)-1; // Since we are counting from 0, we need to
// adjust the last position accordingly
While($posFirst <= $posLast)
{
$posFirst = strpos($number, "1", $posFirst);
echo $posFirst."<p>" ;
$posFirst = $posFirst+1;
}
?>
</body>
</html>
[/code]
The output I get is an infinite loop and I don't get what's causing it to loop forever.
0
2
5
2
5
2
5
2
5
2
5
2
Any idea would be appreciated. Thanks.
Comment