while 循环在 PHP 代码中执行无限次

while loop in the PHP code excecuting infinite times

<?php
$string="I am Azizul hakim.I am a student.I am feeling good today";
$find="am";
$i=0;
$find_length=strlen($find);
while($i<strlen($string))
{
    $pos=strpos($string,$find,$i);
    echo $pos;
    $i=$pos+$find_length;
}
?>

php代码无限执行times.Although限制是通过指定字符串长度限制的string.Why它正在无限执行?

strpos returns false,当您剩余的 $string 不包含 $find 并且循环重新开始时,计算结果为 0。

在这种情况下,我相信 perl 兼容的正则表达式 (pcre) 始终是最佳选择。为什么,因为您将仅使用 (1) 函数/方法调用,然后使用简单的循环来处理结果。使用字符串类型函数/方法,每次在 $haystack 中找到您的 $needle 时,您都需要调用另一个字符串类型函数/方法来处理下一次在 $haystack 中找到您的 $needle!