Jump to content

Is There An Easier Way To Remove Small Words From A String?


astralaaron

Recommended Posts

Hi I am trying to take a string like: "Arnold Schwarzenegger is the Terminator" and strip out words that are less than 4 characters long, so the output would be: "Arnold Schwarzenegger Terminator" I am thinking to explode the string to an array then do a loop on each word to check the length. I was just wondering if there is a simpler way that I am over looking?

Link to comment
Share on other sites

I think the best thing to do is to make a dictionary of all the words that you might want to strip from a string. You wouldn't want to remove "cat" just because it's only three letters.

Link to comment
Share on other sites

function removeSmallWords($str){$removeThese  = array('in','or');$words = explode(" ",$str);$newString = ""; foreach($words as $word){  $word = str_replace($removeThese,'',$word);  $newString.=$word;}  return $newString;}

Hi I am having a problem with the idea suggested.. when I try something like this: $string = "Windows or Doors";$string = removeSmallWords($sting);echo $string; it returns "Wdows Dos" I want to be able to remove "a, I, my, it, at" etc... they are all in bigger words though so it is a problem how I am doing this. Do I need to create a loop that runs a loop for each word on my list of words that I want removed? EDIT: Ahh ignore this it was a stupid question. I just need to check the strlen() first and go from there...

Link to comment
Share on other sites

If you want to use str_replace then put spaces around the words. Check for " or " instead of just "or". That won't cover words that appear at the end of the sentence though.
Hey I thought about this.. then I thought that I would need "or " in case they started the string with the words, and I was thinking that I would have the same problem because "door " for example would match it.. wouldn't it? I guess I could add a space to the beginning of the string to stop that, right?
Link to comment
Share on other sites

Instead of several cases you can use regular expressions to look for a pattern. So "or" would either need to be at the start of the string or have a space before it, and after it there should be a space, period, comma, exclamation, question mark, etc, or be at the end of the string. Of course, this all assumes that people are typing with correct grammar. If people use punctuation without a space before the next word then that's a whole other issue.

Link to comment
Share on other sites

Instead of several cases you can use regular expressions to look for a pattern. So "or" would either need to be at the start of the string or have a space before it, and after it there should be a space, period, comma, exclamation, question mark, etc, or be at the end of the string. Of course, this all assumes that people are typing with correct grammar. If people use punctuation without a space before the next word then that's a whole other issue.
yeah I was thinking about punctuation also, I am going to replace all of that with a space to make sure "movies!and popcorn" type of problem will not happen thanks a lot for the help
Link to comment
Share on other sites

Archived

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

×
×
  • Create New...