2017-08-26 7 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 ; 
} 
?> 

は、針が(オフセットとは無関係に)文字列haystackの先頭からの相対存在する場所の位置を返します。また、文字列の位置は0ではなく1から始まることに注意してください。

針が見つからない場合はFALSEを返します。

あなたのコードは無限ループを作成すると述べたが、そうではない。 strposは、正しく使用した第3のパラメータとしてoffsetを受け入れます。各反復でオフセットが増分され、最後に見つかった結果文字列が終了する位置で新しい検索が開始され、無限ループが回避されます。

+0

なぜdownvote?理由? –

+0

ありがとうございました –

+0

私は$ effectをオフセットに変更しました –

関連する問題