使用preg_match查找列表中的所有单词

| 在SO的帮助下,我能够从电子邮件主题行中提取“关键字”以用作类别。现在,我已决定允许每张图片使用多个类别,但似乎无法正确表达我的问题以得到Google的良好回应。 preg_match在列表的第一个单词处停止。我确定这与“渴望”或只是将管道符号
|
换成其他符号有关,但我看不到它。     
\\b(?:amsterdam|paris|zurich|munich|frankfurt|bulle)\\b
。 我当前使用的整个字符串是:
preg_match(\"/\\b(?:amsterdam|paris|zurich|munich|frankfurt|bulle)\\b/i\", \".\" . $subject . \".\", $matches);
我需要做的就是将所有这些单词(如果存在)撤出,而不是在阿姆斯特丹停留,或者在搜索的主题中首先出现的任何单词。之后,只需处理$ matches数组,对吧? 谢谢, 标记     
已邀请:
        好的,下面是一些带有
preg_match_all()
的示例代码,该代码还显示了如何删除嵌套:
$pattern = \'\\b(?:amsterdam|paris|zurich|munich|frankfurt|bulle)\\b\';
$result = preg_match_all($pattern, $subject, $matches);

# Check for errors in the pattern
if (false === $result) {
    throw new Exception(sprintf(\'Regular Expression failed: %s.\', $pattern));
}

# Get the result, for your pattern that\'s the first element of $matches
$foundCities = $result ? $matches[0] : array();

printf(\"Found %d city/cities: %s.\\n\", count($foundCitites), implode(\'; \', $foundCities));
由于ѭ5现在是一个简单的数组,因此您也可以直接对其进行迭代:
foreach($foundCities as $index => $city) {
    echo $index, \'. : \', $city, \"\\n\";
}
不需要嵌套循环,因为返回值“ 7”已经被标准化。其概念是使代码根据需要返回/创建数据以进行进一步处理。     

要回复问题请先登录注册