Skip to content

Instantly share code, notes, and snippets.

@mustafatoker
Last active February 13, 2017 10:04
Show Gist options
  • Save mustafatoker/9aba72556dfaa684715d3f4b4df60f17 to your computer and use it in GitHub Desktop.
Save mustafatoker/9aba72556dfaa684715d3f4b4df60f17 to your computer and use it in GitHub Desktop.
Increment integer in string
<?php
function increment($matches) {
return ++$matches[1];
}
$string = 'value1';
$string = preg_replace_callback( "|(\d+)|", 'increment', $string);
echo $string; // value2
// if u want to use in same class
$string = preg_replace_callback( "|(\d+)|", [$this, 'increment'], $string);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment