SJames Posted September 5, 2007 Share Posted September 5, 2007 Hi, I'm trying to use str_replace to remove certain words from search terms with my website search. For example, when the user enters "term and word", it would remove "and" and only search for "term" and "word". I have the following list of words I want to remove: "And", "Or", "If", "We", "An", "A", "I" The problem is that if the user enters something like "Anderson", the term would be replaced with "erson" because the and is removed. Is there a way to remove only certain letters when they are by themselves? For example, change "Anderson and Bill" to "Anderson", "Bill" - instead of "erson", "Bill". I have tried to solve this by adding spaces around the words to replace. This still doesn't work, however, if the user enters something like "An owl" or "I like music". Quote Link to comment https://forums.phpfreaks.com/topic/68010-str_replace-help/ Share on other sites More sharing options...
ToonMariner Posted September 5, 2007 Share Posted September 5, 2007 $remove = array("And", "Or", "If", "We", "An", "A", "I"); $str = str_replace($remove,'',$str); Quote Link to comment https://forums.phpfreaks.com/topic/68010-str_replace-help/#findComment-341928 Share on other sites More sharing options...
Jessica Posted September 5, 2007 Share Posted September 5, 2007 Toon did you read it? SJames, what I would do is check if there is a space on both sides. Then, check if the words at the beginning of the sentence or the end are those words. Those are the only cases where it won't have a space on both sides. You had the right idea you just need to add that bit of logic. If you need help with that part still post the code that worked except for "An Owl" and we'll work on it together! Quote Link to comment https://forums.phpfreaks.com/topic/68010-str_replace-help/#findComment-341944 Share on other sites More sharing options...
jitesh Posted September 5, 2007 Share Posted September 5, 2007 This is one solution <?php $str = "I say This is a string And String . If It has multiple chars."; $str_array = explode(" ",$str); $new_array = array(); foreach($str_array as $key => $value){ if(!preg_match("/^And$|^Or$|^If$|^We$|^An$|^A$|^I$/i",$value)){ $new_array[] = $value; } } echo $str; echo "<br>"; echo implode(" ",$new_array); ?> Quote Link to comment https://forums.phpfreaks.com/topic/68010-str_replace-help/#findComment-341994 Share on other sites More sharing options...
ToonMariner Posted September 5, 2007 Share Posted September 5, 2007 OOPS sorry - Jessie not properly!!!! jitesh has saved the day... Quote Link to comment https://forums.phpfreaks.com/topic/68010-str_replace-help/#findComment-342050 Share on other sites More sharing options...
d22552000 Posted September 5, 2007 Share Posted September 5, 2007 first of all use ireplace so its NOT case sensative second of all just do " And ", " Or ", " If ", " We ", " An ", " A ", " I " Using explode on large bodies of text (5k-50k characters) is a no-no 0-,-0 Quote Link to comment https://forums.phpfreaks.com/topic/68010-str_replace-help/#findComment-342064 Share on other sites More sharing options...
Crow Posted September 5, 2007 Share Posted September 5, 2007 I don't think he's going to allow 5k - 50k characters for his search terms. Quote Link to comment https://forums.phpfreaks.com/topic/68010-str_replace-help/#findComment-342219 Share on other sites More sharing options...
cooldude832 Posted September 5, 2007 Share Posted September 5, 2007 I think that the regular expression match is a bit over doing it, there is a way to test it better as for exploding its fine because its a query string as he said for a search which isn't going to be greater than 500 characters (or 100 words) which will take almost no time to do. <?php $bad_words = array("and","or","an","a"); $query = "We think we are great and a super cool team"; $test = explode(" ",$query); foreach($test as $value); if(in_array($value,$bad_words)){ $string[] = ""; } else{ $string[] = $value; } $clean_query = implode(" ",$string); //Should now be We think we are great super cool team ?> regular expression uses a lot more resources than you need Quote Link to comment https://forums.phpfreaks.com/topic/68010-str_replace-help/#findComment-342221 Share on other sites More sharing options...
sasa Posted September 5, 2007 Share Posted September 5, 2007 try <?php $remove = "And|Or|If|We|An|A|I|"; $a = 'Anderson and Bill oR Orr'; $b = preg_replace('/[^a-z0-9_](?:'.$remove.')([^a-z0-9_]|$)/i',"$1",$a); echo $b; ?> Quote Link to comment https://forums.phpfreaks.com/topic/68010-str_replace-help/#findComment-342274 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.