Jump to content

take out repeating words


dcole.ath.cx

Recommended Posts

What script could I use to remove parts of an array if it's repeated?like$test = "I wish I was a moderator. I wish I was a bus driver. I love smiles!$data = explode(" ", $test);SOMETHING---then the new array would be: I, wish, was, a, moderator, bus, driver, love, smiles!As you can see the repeat of a word was taken out.

Link to comment
Share on other sites

I believe this code may suit your requirements.-------------------------------------------------------------------------------------------$test = "I wish I was a moderator. I wish I was a bus driver. I love smiles!";$data = explode(" ", $test);$count = count($data);$newdata = array();for($i=0; $i<$count; $i++){ $value = $data[$i]; if(!in_array($value, $newdata)) { $value = explode(".", $value); $newdata[] = $value[0]; }}echo print_r($newdata);-------------------------------------------------------------------------------------------Regardssrinivasan and suresh (Team Crew)India - Tamilnadu

Link to comment
Share on other sites

That code works. But you don't need to do an echo before print_r, print_r does not return anything, it just sends it straight to output. Just writing print_r is enough to see the array.But that's beside the point, that code does work.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...