开发者

To remove duplicate word in a string in php

I want to remove duplicate words in a string.

For example:

$str="oneone"; (without spaces)

I want to remove dupl开发者_如何学Goicates and display as one.

Can anyone suggest me a solution?


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);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜