preg match all - PHP check if two keywords occur in String -
my challange explained on following example: keyword combination "gaming notebook" given.
i want check whether 2 keywords occur in string. challange string this:
"nice gaming notebook" "notebook gaming" "notebook extreme gaming"
i want function return true of 3 strings. there tolerance of 3-4 words can between word combination , examples show, want work if keywords switched.
so approach following, not seem work:
$keyword = strtolower("gaming notebook"); $parts = explode(" ", $keyword); $string = strtolower("which notebook gaming performance"); //point end of array end($parts); //fetch key of last element of array. $lastelementkey = key($parts); //iterate array $searchexpression = ""; foreach($parts $k => $v) { if($k != $lastelementkey) { $searchexpression .= $v . "|"; } else { $searchexpression .= $v; } } if(preg_match_all('#\b('. $searchexpression .')\b#', $string, $matches) > 0) { echo "jep, keyword combination in string"; } else { echo "no, keyword combination not in string"; }
you want use cmu sphinx or natural language index in database. (see http://dev.mysql.com/doc/refman/5.7/en/fulltext-natural-language.html) doing quick search of php libraries turned "nlp-tools/nlp-tools," however, have never used pure php solution accomplish trying do.
Comments
Post a Comment