views:

129

answers:

1

I want to remove duplicate words in a string.

For example:

$str="oneone"; (without spaces)

I want to remove duplicates and display as one.

Can anyone suggest me a solution?

+1  A: 

I'm not sure what you're asking. As others have said, it would be difficult to remove all duplicates. But if you just want words that contain only duplicates (for example, you want to change "oneone" to "one", but leave "everyoneone" as it is), the simplest thing to do would be to check for words with an even number of letters, where the second half is the same as the first half.

Split the text into words, and for each word do something like

$length = strlen($word);
if (! $length % 2 && substr($word, 0, $length / 2) == substr($word, ($length / 2) + 1, $length /2))
    $word = substr($word, 0, $length / 2);
A. M.