Skip to content

Instantly share code, notes, and snippets.

@sobstel
Created April 10, 2013 21:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sobstel/5358541 to your computer and use it in GitHub Desktop.
Save sobstel/5358541 to your computer and use it in GitHub Desktop.
preg_replace_callback with match counter
<?php
$counter = 0;
$result = preg_replace_callback(
'#\?#',
function ($match) use (&$counter) {
$counter += 1;
return $counter;
},
'a: ?, b: ?, c: ?, d: ?. e: ?'
);
echo $result;
// => a: 1, b: 2, c: 3, d: 4. e: 5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment