tags:

views:

28

answers:

2

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 combine 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.

A: 
$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.

Cups
try your code with `$str = "bandits in buckethats";`
stereofrog
ha, something about 'bandits and buckethats' is funny. I'd like to read that article.
pfunc
I guess stereofrog's got a point there, you could use explode() on spaces to generate an array of words, then knock the stop words out of the array. Useful too if your initial search turns up nothing or little, you can then optionally do a traditional like "%$word%" search.
Cups
A: 

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))....
stereofrog