user1055645
user1055645

Reputation:

Php - Remove repeated words in a string

Input: ball ball code

Output should be: ball code

Input: awycodeawy

Output should be: awycode

I tried these, but didn't work:

$q = preg_replace("/\s(\w+\s)\1/i", "$1", $q);
$q = preg_replace("/s(w+s)1/i", "$1", $q);

Upvotes: 1

Views: 4124

Answers (2)

anubhava
anubhava

Reputation: 785008

Here is positive lookahead base attempt on regex based solution to OP's problem.

$arr = array('ball ball code', 'abcabc bde bde', 'awycodeawy');
foreach($arr as $str)
   echo "'$str' => '" . preg_replace('/(\w{2,})(?=.*?\\1)\W*/', '', $str) ."'\n";

OUTPUT

'ball ball code' => 'ball code'
'abcabc bde bde' => 'abc bde'
'awycodeawy' => 'codeawy'

As you can for the input 'awycodeawy' it makes it to 'codeawy' instead of 'awycode'. The reason is that it is possible to find a variable length lookahead something which is not possible for lookbehind.

Upvotes: 5

Amber
Amber

Reputation: 526573

$q = preg_replace("/\b(\w+)\s+\\1\b/i", "$1", $q);

Upvotes: 4

Related Questions