我如何不能仅将“软件”与“软件和框架”相匹配?
$skill = array("software & framework","communication skills","technology-based","software","technology");
$text = "software & framework and technology-based and excel technology- communication";
foreach ($skill as $skills) {
preg_match_all("~(?<![\w-])" . preg_quote($skills) . "(?![\w-])~i", $text, $matchWords);
foreach ($matchWords[0] as $matchWord) {
echo "<b>MatchWord:</b> " . $matchWord. "<br>";
}
当前结果:
预期结果:
答案 0 :(得分:1)
您需要动态构建带有交替的单个模式:
xtl
然后,您可以提取匹配项:
$pattern = '~(?<![\w-])(?:' . implode('|', array_map(function($i) {
return preg_quote($i, '~');
}, $skill)) . ')(?![\w-])~i';
输出:if (preg_match_all($pattern, $text, $matchWords)) {
print_r($matchWords[0]);
}
注意:如果Array ([0] => software & framework [1] => technology-based )
数组中的迭代没有按长度降序排列,则需要使用以下方法进行预先设置,例如:
$skill