title: How can I find, increment and replace in php?
body: <p>I have strings in the form <code>\d+_\d+</code> and I want to add 1 to the second number. Since my explanation is so very clear, let me give you a few examples:</p>
  
  <ul>
  <li>1234567_2 should become 1234567_3</li>
  <li>1234_10 should become 1234_11</li>
  </ul>
  
  <p>Here is my first attempt:</p>
  
  <pre><code>$new = preg_replace("/(\d+)_(\d+)/", "$1_".((int)$2)+1, $old);
  </code></pre>
  
  <p>This results in a syntax error: </p>
  
  <blockquote>
    <p>Parse error: syntax error, unexpected
    T_LNUMBER, expecting T_VARIABLE or '$'
    in [...] on line 201</p>
  </blockquote>
  
  <p>Here is my second attempt</p>
  
  <pre><code>$new = preg_replace("/(\d+)_(\d+)/", "$1_".("$2"+1), $old);
  </code></pre>
  
  <p>This transforms $old = 1234567_2 into $new = 1234567_1, which is not the desired effect</p>
  
  <p>My third attempt</p>
  
  <pre><code>$new = preg_replace("/(\d+)_(\d+)/", "$1_".((int)"$2"+1), $old);
  </code></pre>
  
  <p>This yeilds the same result.</p>
  
  <p>By making these attempts, I realized I didn't understand how the new $1, $2, $3, .. variables really worked, and so I don't really know what else to try because it seems that these variables no longer exist upon exiting the preg_replace function...</p>
  
  <p>Any ideas?</p>