Q:

Codeigniter preg_replace_callback

I would like for preg_replace_callback to use a library function from CodeIgniter as its callback. My current unsuccessful attempt is the following:
$content = preg_replace_callback('/href="(\S+)"/i',
    '$this->util->url_to_absolute("http://www.google.com","$matches[0]")',
    $content);

But I haven't had any success. I've tried using create_function, but I can't get that to work either. Any help would be greatly appreciated.

A:

As of php 5.3
$that = $this;
$content = preg_replace_callback($patt, function($matches) use ($that) {
    return $that->util->url_to_absolute("http://www.google.com", $matches[1]);
}, $content);

//or
$that = $this->util;
$content = preg_replace_callback($patt, function($matches) use ($that) {
    return $that->url_to_absolute("http://www.google.com", $matches[1]);
}, $content);

//or
$callback = array($this->util, 'url_to_absolute');
$content = preg_replace_callback($patt, function($matches) use ($callback) {
    return call_user_func($callback, "http://www.google.com", $matches[1]);
}, $content);

