我有一個數組中的單詞列表。我需要在字符串上查找任何這些單詞的匹配。PHP正則表達式匹配一個字符串列表
示例單詞列表
company
executive
files
resource
例串
Executives are running the company
下面是我寫的功能,但它不工作
$matches = array();
$pattern = "/^(";
foreach($word_list as $word)
{
$pattern .= preg_quote($word) . '|';
}
$pattern = substr($pattern, 0, -1); // removes last |
$pattern .= ")/";
$num_found = preg_match_all($pattern, $string, $matches);
echo $num_found;
輸出
0
你對這個例子期望輸出什麼? – Gumbo 2010-10-13 06:20:34