Instantly share code, notes, and snippets.

Embed
What would you like to do?
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