$text = $_POST['text'];
$find = $_POST['find'];
$offset = 0;
while ($offset < strlen($text))
{
$pos = strpos($text, $find, $offset);
$offset += strlen($find);
echo "$find found in $pos <br>";
}
There is something wrong with this program. All I want to do is print all the positions in which $find is located in $text.
Tell me what the problem is. Try not to change the while condition. Thanks in advance.
First of all you need to break out of your loop if its not found. And secondly, what I think you want to do is to jump to the point in the $text just after where you found the last $find:
$text = $_POST['text'];
$find = $_POST['find'];
$offset = 0;
while ($offset < strlen($text))
{
$pos = strpos($text, $find, $offset);
// $offset += strlen($find); // removed
if ( $pos===false ) break;
$offset = $pos+1;
echo "$find found in $pos <br>";
}