Regex 匹配字符串中的单词

Regex 匹配字符串中的单词,regex,Regex,我有下一个代码: $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);

我有下一个代码:

$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=“大家好”;
$strings\u compare=明天,你好,世界;
$string_array=split(“,”,$strings_compare);
对于($i=0;$i请尝试以下方法:

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


当我使用有效的数组语法时,它很适合我

<?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";
    }
}

array()不()…
array(“明天”、“你好”、“世界”)
。另外,将
preg\u匹配(“/$string\u数组[$i]/”,$string);
更改为
preg\u匹配(“/”{$string\u数组[$i]>,$string)
我已经更新了我的代码..谢谢你说的对,preg_match函数不是错误,但是为什么我的错误是:$strings_compare=明天,你好,world;$string_array=split(“,”,$strings_compare)??我看不到。好的,非常感谢。你的帮助对我非常有用。我将回顾如何在数组中转换$string\u数组,再次感谢。最后,这是我的代码:$strings\u compare=明天,你好,world;$string\u数组=explode(',,$strings\u compare);在bucle中,我删除了白色$string\u数组中的空格[$I]=trim($string_数组[$i]),现在对我有效。感谢您的帮助。
<?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";
    }
}