title: PHP preg_replace occurrence of any text after the very last underscore
body: <p>I can do this using <code>explode</code> (or have fun with with <code>strrpos</code>), but i prefer to using <code>preg_replace</code> because should be a bit faster, i think (isn't it?). Plus, it's concise and elegant.</p>
  
  <p>The purpose is, given a string like <code>a_b_c</code> to obtain another sting <strong>where characters following the last <code>_</code></strong> where substituted with passed string.</p>
  
  <p>I'm not good at regular expression. I've to find the time to buy a good book at study. Anyway i've tried this regex <code>'/_(.*)$/'</code> as <em>match the end of the string, capture any character following the last underscore</em>.</p>
  
  <p>What's wrong in my argumentation?</p>
  
  <pre><code>// Do it using explode
  function foo($string, $replacement)
  {
      $pieces = explode('_', $string);
      array_pop($pieces);
      return implode('_', array_merge($pieces, array($replacement)));
  }
  
  // Do it using regular expression (not working)
  function bar($string, $replacement)
  {
      return preg_replace('/_(.*)$/', $replacement, $string);
  }
  
  echo foo('a_b_c', 3); // Prints a_b_3
  echo bar('a_b_c', 3); // Prints a3 wrong!!!
  </code></pre>