Here's one way to do it in PHP. There are possibly better ways.
#!/usr/bin/php -q
<?php
error_reporting(E_ALL);
$data = array("one", "foo", "two", "foo",
"three", "foo", "four",
"foo", "foo");
$changed = 0;
$new = preg_replace_callback("/foo/",
create_function(
'',
'$GLOBALS{"changed"}++;
return "bar";'
),
$data);
print_r($new);
print "We changed $changed items\n";
?>
This prints
Array
(
[0] => one
[1] => bar
[2] => two
[3] => bar
[4] => three
[5] => bar
[6] => four
[7] => bar
[8] => bar
)
We changed 5 items
I hope this helps satisfy your curiosity