Codeigniter preg_replace_callback [英] Codeigniter preg_replace_callback

查看:128
本文介绍了Codeigniter preg_replace_callback的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要 preg_replace_callback 使用来自CodeIgniter的库函数作为其回调。我目前的失败尝试如下:

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);

但我没有成功。我尝试使用 create_function ,但我也不能让它工作。

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.

推荐答案

截至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);

这篇关于Codeigniter preg_replace_callback的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆