title: Improve my regex+php replacement
body: <p>I'm trying to replace a string by a part of it with regex. My code do the job, but it is the right way?</p>
  
  <pre><code>$string = 'blabla &lt;!-- s:D --&gt;&lt;img src="{SMILIES_PATH}/icon_biggrin.gif" alt=":D" title="Very Happy" /&gt;&lt;!-- s:D --&gt; blabla &lt;!-- scat --&gt;&lt;img src="{SMILIES_PATH}/cat2.gif" alt="cat" title="Cat" /&gt;&lt;!-- scat --&gt; blabla';
  $pattern = '(&lt;!-- s(\S*) --&gt;&lt;img src="\S*" alt="\S*" title="[^"]+" \/&gt;&lt;!-- s\S* --&gt;)';
  
  preg_match_all($pattern, $string, $result);
  
  $i = 0;
  foreach ($result[0] as $match) {
      $string = str_replace($match, $result[1][$i], $string);
      $i++;
  }
  </code></pre>
  
  <p>What I want: blabla <code>:D blabla cat blabla</code></p>
  
  <p>Regex test: <a href="https://regex101.com/r/fD0xI2/2" rel="nofollow">https://regex101.com/r/fD0xI2/2</a></p>
  
  <p>PHP test: <a href="http://ideone.com/mrS0BJ" rel="nofollow">http://ideone.com/mrS0BJ</a></p>