for example, i have these few keywords
$word = "word1,word2,word3";
and substract/explode that using $keyword = explode(",", trim($word));
then ill get these $keyword[0] $keyword[1] and $keyword[2]
now, how to match these keyword using preg_match?
$others = "this is example where there is word1";
if(preg_match('/($keyword[0]|$keyword[1]|$keyword[2])/i', $others)){
echo "matched";
}
the problem is, if there is only 2 words, it will matched all the words in $others.
is there any more propper or easier and efficient way to do it?
$rx = '/' . implode("|", array_map(function($i) { return preg_quote(trim($i), "/"); }, explode(",", $word))) . '/i';, then usepreg_match($rx, $others), see demo. Add word boundaries,\b, on both ends to only match whole words. Or, better,(?<!\w)at the start and(?!\w)at the end.