title: Remove repetitve Consecutive words from string
body: <p>I want to remove repetitive words from the string (only consecutive).</p>
  
  <pre><code>$str = 'abc,def,fgh,fgh,xna,fgh,xyz,xyz,xyz,tr,tr,xna';
  </code></pre>
  
  <p>My desired output string is:</p>
  
  <pre><code>abc,def,fgh,xna,fgh,xyz,tr,xna
  </code></pre>
  
  <p>I can get the result I want in php using this:</p>
  
  <pre><code>$ip = explode(',', $str);
  $op = [];$last = null;
  for($i=0;$i&lt;count($ip);$i++){
      if ($last == $ip[$i]) {
          continue;
      }
      $op[]=$last=$ip[$i];
  }
  $ip = implode(',', $op);
  </code></pre>
  
  <p>But was looking for the regex approach. So far I have got closer with these two regexes:</p>
  
  <pre><code>$after = preg_replace('/(?:^|,)([^,]+)(?=.*,\1(?:,|$))/m', '', $str);
  output : abc,def,fgh,xyz,tr,xna
  
  $after = preg_replace('/([^,]+)(,[ ]*\1)+/m', '', $str);
  output : abc,degh,fgh,xna,fgh,,,xna
  </code></pre>