2017-08-26 162 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

我改變$效果抵消 –