2017-08-26 161 views
-6
<?php 
$efFect=0; 
$find='is'; 
$find_len=strlen($find); 
$string22 ='this is space is function'; 

while ($str_position =strpos($string22,$find,$effect)){ 
    echo $find.'the postion in '. $str_position.'<br>'; 
    $efFect = $str_position + $find_len ; 
} 
?> 
+0

您能否让代码更清洁?你想达到什么目的? –

+0

问题是什么?你会得到什么结果?说明? – Yarimi

+0

您在这里创建一个无限循环 –

回答

0

0是一个虚假值。你应该通过检查strpos是否返回false来使你的检查更可靠。

<?php 
$effect=0; // not $efFect as variables names are case sensitive in PHP 
$find='is'; 
$find_len=strlen($find); 
$string22 ='this is space is function'; 

while (($str_position = strpos($string22, $find, $effect)) !== False){ 
    echo $find.'the postion in '. $str_position.'<br>'; 
    $effect = $str_position + $find_len ; 
} 
?> 

从文档:

返回将针存在相对于干草堆字符串的开头(独立的偏移)的位置。另请注意,字符串位置从0开始,而不是1.

如果未找到针,则返回FALSE。

有人提到你的代码创建了一个无限循环,但事实并非如此。 strpos接受offset作为您正确使用的第三个参数。在每次迭代时,偏移量都会增加,这样新的搜索将从最后找到的结果串结束的位置开始,从而避免无限循环。

+0

为什么downvote?原因? –

+0

谢谢你所有的工作 –

+0

我改变$效果抵消 –