1

I have an array of stop words that i would like removed from a string. But in every case the words like 'in' are being removed from valid words like climbing leaving climb g in the resulting string.

I was thinking if I could have two arrays. One array of the stop words and one array of the exploded string. Is there a quick way to use the array of stop words to compare against the array of the exploded string, thus removing all stop words from the exploded string array...

$stopwords = array( 'foo', 'bar');
$string = "foo bar Foobar";
$newArray = explode(" ", $string)

I would like the resulting $string = "Foobar"?

thanks in advance,

Marv

2
  • is this a poor attempt to censor swear words? Commented Jul 25, 2013 at 22:08
  • nope... doing a student project on clustering Commented Jul 25, 2013 at 22:46

2 Answers 2

2

array_diff is your friend to eliminate the words you don't like too see:

$string = implode(" ", array_diff(explode(" ", $string), $stopwords));
Sign up to request clarification or add additional context in comments.

1 Comment

@user2620610 then please mark the green bordered tick on the left under my answer's score to accept it.
2

You can use array_diff, it will return you the difference between first and second array. For conversion to string, you must make sure you have only 1 last element (with count, for instance)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.