我有一个数组中的单词列表。我需要在字符串上查找任何这些单词的匹配。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