- preg_replace_callback: documentation ( source)
<?php
class Foo
{
function escape($str)
{
return '`' . $str . '`';
}
function bar()
{
$str = 'hello world !';
$cb = array($this, 'escape');
return preg_replace_callback('~\w+~', function($m) use ($cb) {
return call_user_func($cb, $m[0]);
}, $str);
}
}
$foo = new Foo();
echo $foo->bar();