Jump to content

str_replace help


SJames

Recommended Posts

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

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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);
?> 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.