zartzar Posted May 13, 2009 Share Posted May 13, 2009 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 More sharing options...
Ken2k7 Posted May 13, 2009 Share Posted May 13, 2009 Okay? I don't see the problem there. What do you want to get? Link to comment https://forums.phpfreaks.com/topic/158001-removing-stop-words-with-str_replace/#findComment-833429 Share on other sites More sharing options...
zartzar Posted May 13, 2009 Author Share Posted May 13, 2009 Okay? I don't see the problem there. What do you want to get? If i type in: "the man was a hero for saving the child" I want: "man hero saving child" not "mn ws hero sving child" Link to comment https://forums.phpfreaks.com/topic/158001-removing-stop-words-with-str_replace/#findComment-833448 Share on other sites More sharing options...
zartzar Posted May 13, 2009 Author Share Posted May 13, 2009 Anyone? :'( Link to comment https://forums.phpfreaks.com/topic/158001-removing-stop-words-with-str_replace/#findComment-833484 Share on other sites More sharing options...
Axeia Posted May 13, 2009 Share Posted May 13, 2009 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 ) Link to comment https://forums.phpfreaks.com/topic/158001-removing-stop-words-with-str_replace/#findComment-833559 Share on other sites More sharing options...
Ken2k7 Posted May 13, 2009 Share Posted May 13, 2009 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; Link to comment https://forums.phpfreaks.com/topic/158001-removing-stop-words-with-str_replace/#findComment-833564 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.