-2

次のコードがあります:

$string = "hello to all world";
$strings_compare = tomorrow, hello, world;
$string_arrays =split(',',$strings_compare);

      for ($i=0; $i<count($string_arrays); $i++){
          $resultado = preg_match("/$string_arrays[$i]/",$string);
             if($resultado == false){   
             echo "no match";              
             }else {
            echo "match";
             }
      }

このコードでは、結果は次のようになります:一致なし、一致、一致なし

結果は次のようになります:一致なし、一致、一致。私のエラーは何ですか?

結果が一致、一致、一致$stringによって変更された場合、これはOKです。$string='say hello to all world now'

4

2 に答える 2

1

有効な配列構文を使用している場合は、問題なく機能します。

<?php
$string = "hello to all world";
$string_arrays = array("tomorrow", "hello", "world");

for ($i=0; $i<count($string_arrays); $i++) {
    $resultado = preg_match("/$string_arrays[$i]/",$string);
    if(!$resultado) {   
        echo "no match";              
    } else {
        echo "match";
    }
}

戻り値

matchmatchmatchはありません

于 2012-09-18T11:56:30.923 に答える
0

これを試して:

$resultado = preg_match("/".preg_quote($string_arrays[$i])."/",$string);

また:

$string_arrays = array("tomorrow", "hello", "world");
于 2012-09-18T11:53:39.733 に答える