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);
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";
'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.
$q = preg_replace("/\b(\w+)\s+\\1\b/i", "$1", $q);
\s+ in the middle to a \s*.book into bok, or mississippi into missippi).awycodeawy -> awycode.
aabaaabaabaaabecome?