combining dynamic text with regular expressions in php
I am experimenting with finding popular keywords using curl, php and regular expressions. I have an array of non-specific nouns that I am matching my keyword search up. So I am looking for words like "the", "and", "that" etc. and taking them out of the keyword search.
so I have an array of words like so:
$wordArr = [the, and, at,....];
and then running something like:
&& preg_match('(\bmyword\w*\b)', $key) == false
how do I combin开发者_开发知识库e these two so it loops through the array finding out if any of the words in the array match the regular expression?
I guess I could just do a for loop, but though maybe I could use in_array($wordArr, $key)
.. or something like that.
$str = "cars and all that stuff";
$a = array("and", "that");
$b = str_replace( $a, '', $str );
echo $b ;
"cars all stuff"
Favour PHP native functions for this when you can, faster.
If the blacklist is not very long (say, lesser than 100 entries), you can build one big regexp out of it:
$stops = '~\\b(' . implode('|', $stopWords) . ')\\b~';
if(preg_match($stops, $text))....
精彩评论