You are here:  » Find & Replace Duplicate Consecutive Words

Active Forum Topics

Find & Replace Duplicate Consecutive Words

Submitted by nanaz on Wed, 2018-10-10 08:56 in

Hi David,

Hope you are okay!

I'm trying to make a filter to find duplicate consecutive words in Productnames and only remove the surplus words.

For example:

'Flik Flak Watch watch KW239' should be 'Flik Flak watch KW239'
'Skagen skagen watch FL23H2' should be 'Skagen watch FL23H2'

I found a regexp

$string=preg_replace('/\b(\S+)(?:\s+\1\b)+/i', '$1', $string);

but I can't seem to find the right way to implement it.

A custom filter in filter.php would be great but I haven't succeeded in making one.

Hope you can help!

Cheers, Marlies

Submitted by support on Wed, 2018-10-10 10:34

Hello Marlies,

Have a go with the following; add to includes/filter.php to add a new "Strip Duplicate Words" filter...

  /* stripDupes */
  $filter_names["stripDupes"] = "Strip Duplicate Words";
  function filter_stripDupesConfigure($filter_data)
    print "<p>There are no additional configuration parameters for this filter.</p>";
  function filter_stripDupesValidate($filter_data)
  function filter_stripDupesExec($filter_data,$text)
    $words = explode(" ",$text);
    $newWords = array();
    foreach($words as $word)
      $newWords[strtolower($word)] = $word;
    return implode(" ",$newWords);

Hope this helps!


Submitted by nanaz on Wed, 2018-10-10 17:53

Hi David,

It works great, you're the best!

So much better than the regexp I used which often doesn't work as expected.

Thank you for your amazing support - as always :-)

Cheers, Marlies