-2

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?

deceze
  • 510,633
  • 85
  • 743
  • 889
Rekha
  • 449
  • 2
  • 15
  • 27
  • 1
    possible duplicate of [remove duplicate from string in PHP](http://stackoverflow.com/questions/2613063/remove-duplicate-from-string-in-php) – John Carter Aug 20 '10 at 08:54
  • 1
    Without word boundaries it's going to be tough to define what counts as duplicate: "queuequeue". Also, @there, precisely because there are no word boundaries this is not a duplicate of the linked question. – deceze Aug 20 '10 at 08:56
  • 1
    without spaces ? you should define a minimal wordl length, because, "o" will be duplicate of another "o" same as "one" and "one" – Piotr Müller Aug 20 '10 at 08:57
  • Not to mention those delightful words in the English language such as haha (a ditch), or fruit names like pawpaw – Mark Baker Aug 20 '10 at 09:22
  • 2
    Even if you could come up with an algorithm to compare the longest non-repeating sequences, you would still have to find out whether these sequences contain real duplicates based on the language, for instance "neoneone" contains the words "neo", "one", "eon", "neon" or "on". How does the code decide if it should remove "oneone" or "eoneon" or "neoneo"? Deciding this would require semantic and syntactic analysis of the context. Good luck with that. – Gordon Aug 20 '10 at 09:36
  • why is the question voted down??? – Grumpy Aug 20 '10 at 09:42
  • Could you maybe provide more detail about the actual problem you're trying to solve? There's no easy way to simply remove duplicate strings (see other comments), but there might be a completely different way of tackling the underlying problem if you told us more about it. – anschauung Aug 20 '10 at 09:51
  • 1
    On an additional note: You asked 9 questions on this site and didn't accept any answers. This doesn't motivate people to care (especially when you provide so little input for us). So may i advise you to go back to your older questions and accept some answers ? – edorian Aug 20 '10 at 10:10

1 Answers1

2

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.
  • 580
  • 1
  • 8
  • 21