Jump to content

removing stop words with str_replace


zartzar

Recommended Posts

Im trying to replace stop words with str_replace however its not doing exactly what i want to. I dont want it to remove character from existing words, but only the specified characters. Sorry thats a bad description but her an example: if i type in "anna" and "liam" ill get "nn" and "li"

 

$search = array('a', 'about', 'above', 'above', 'across', 'after', 'afterwards', 'again', 'against', 'all', 'almost', 'alone', 'along', 'already', 'also','although','always','am','among', 'etc...........');
$replace = array('');
$comments = str_replace($search, $replace, $comments);

Link to comment
https://forums.phpfreaks.com/topic/158001-removing-stop-words-with-str_replace/
Share on other sites

Your problem is with the very first value in the array.

'a'

 

Might want to replace that one with ' a ', the spaces do make a difference ;).

In fact you most likely want to do that with all the values, just use your text editors search and replace function.

Search for ', ' and replace it with the same thing with a space infront and behind it. (saves some manual editting ;))

Or I would do it manually.

 

$search = array('a', 'about', 'above', 'above', 'across', 'after', 'afterwards', 'again', 'against', 'all', 'almost', 'alone', 'along', 'already', 'also','although','always','am','among', 'etc...........');

$comments = explode(' ',$comments);
foreach ($comments as $key => $word) {
     if (in_array($word, $search)) $comments[$key] = '';
}
$comments = implode(' ',$comments);

echo $comments;

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.