我会使用标题中的单词索引并测试每个搜索项是否在该索引中:
$terms = explode(' ', 'great leather shoes');
$titles = array(
'Get Some Really Great Leather Shoes',
'Leather Shoes Are Great',
'Great Day! Those Are Some Cool Leather Shoes!',
'Shoes, Made of Leather, Can Be Great'
);
foreach ($titles as $title) {
// extract words in lowercase and use them as key for the word index
$wordIndex = array_flip(preg_split('/\P{L}+/u', mb_strtolower($title), -1, PREG_SPLIT_NO_EMPTY));
// look up if every search term is in the index
foreach ($terms as $term) {
if (!isset($wordIndex[$term])) {
// if one is missing, continue with the outer foreach
continue 2;
}
}
// echo matched title
echo "match: $title";
}