有没有办法在 preg_replace_callback 回调函数中传递另一个参数?

发布于 2024-08-30 08:19:25 字数 2059 浏览 11 评论 0原文

嗯,伙计们,我真的希望我的英语足够好来解释我需要什么。

让我们以这个代码示例(这只是一个示例!)为例:

class Something(){
    public function Lower($string){
        return strtolower($string);
    }
}
class Foo{
    public $something;
    public $reg;
    public $string;
    public function __construct($reg, $string, $something){
        $this->something = $something;
        $this->reg = $reg;
        $this->string = $string;
    }
    public function Replace(){
        return preg_replace_callback($this->reg, 'Foo::Bar', $this->string);
    }
    public static function Bar($matches){
        /*
        * [...]
        * do something with $matches and create the $output variable
        * [...]
        */

        /*
        * I know is really useless in this example, but i need to have an istance to an object here
        * (in this example, the Something object, but can be something else!)
        */
        return $this->something->Lower($output);
    }
}
$s = new Something();
$foo = new Foo($myregexp, $mystring, $s);
$content = $foo->Replace();

因此,php 手册说要在 preg_replace_callback() 中使用类方法作为回调,该方法一定是抽象的。

我需要在回调函数中传递先前初始化的对象的实例(在示例中为 Something 类的实例)。

我尝试使用 call_user_func (),但不起作用(因为这样我就错过了 matches 参数)。

有没有办法做到这一点,或者让我分开这个过程(在 preg_match_all 之前执行,对于每个匹配检索替换值,然后是一个简单的 preg_replace)?

编辑:作为旁注,在汤姆海格回答之前,我使用了这个解决方法(在示例中,这是替换方法):

$has_dynamic = preg_match_all($this->reg, $this->string, $dynamic);
if($has_dynamic){
    /*
    * The 'usefull' subset of my regexp is the third, so $dynamic[2]
    */
    foreach($dynamic[2] AS $key => $value){
        $dynamic['replaces'][$key] = $this->Bar($value);
    }
    /*
    * ..but i need to replace the complete subset, so $dynamic[0]
    */
    return str_replace($dynamic[0], $dynamic['replaces'], $this->string);
}else{
    return $this->string;
}

希望可以帮助别人。

mmmh guys, i really hope my english is good enaught to explain what i need.

Lets take this example (that is just an example!) of code:

class Something(){
    public function Lower($string){
        return strtolower($string);
    }
}
class Foo{
    public $something;
    public $reg;
    public $string;
    public function __construct($reg, $string, $something){
        $this->something = $something;
        $this->reg = $reg;
        $this->string = $string;
    }
    public function Replace(){
        return preg_replace_callback($this->reg, 'Foo::Bar', $this->string);
    }
    public static function Bar($matches){
        /*
        * [...]
        * do something with $matches and create the $output variable
        * [...]
        */

        /*
        * I know is really useless in this example, but i need to have an istance to an object here
        * (in this example, the Something object, but can be something else!)
        */
        return $this->something->Lower($output);
    }
}
$s = new Something();
$foo = new Foo($myregexp, $mystring, $s);
$content = $foo->Replace();

So, the php manual say that to use a class method as callback in preg_replace_callback(), the method must be abstract.

I need to pass an instance of a previuosly initialized object (in the example, an instance of the Something class) at the callback function.

I tryed to use call_user_func(), but doesnt work (becose in this way i miss the matches parameter).

Is there a way to do that, or have i to separate the process (doing before preg_match_all, for each match retrieve the replace value, and then a simple preg_replace)?

edit: as a side-note, before the tom haigh answer, i used this work-around (in the example, this is the Replace method):

$has_dynamic = preg_match_all($this->reg, $this->string, $dynamic);
if($has_dynamic){
    /*
    * The 'usefull' subset of my regexp is the third, so $dynamic[2]
    */
    foreach($dynamic[2] AS $key => $value){
        $dynamic['replaces'][$key] = $this->Bar($value);
    }
    /*
    * ..but i need to replace the complete subset, so $dynamic[0]
    */
    return str_replace($dynamic[0], $dynamic['replaces'], $this->string);
}else{
    return $this->string;
}

Hope can help someone.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夏花。依旧 2024-09-06 08:19:25

很难将参数传递给回调,但可以使用以下方法代替:

return preg_replace_callback($this->reg, 'Foo::Bar', $this->string);

您可以使 Bar() 不是静态的,并使用它:

return preg_replace_callback($this->reg, array($this, 'Bar'), $this->string);

然后回调函数将能够看到 $this

请参阅 伪类型和变量 中的“回调”

也在 PHP 中>=5.3 你可以使用 匿名函数/闭包 将其他数据传递给回调功能。

It is hard to pass arguments to callbacks, but instead of this:

return preg_replace_callback($this->reg, 'Foo::Bar', $this->string);

You could make Bar() not static, and use this:

return preg_replace_callback($this->reg, array($this, 'Bar'), $this->string);

Then the callback function will be able to see $this

See 'callback' in Pseudo-types and variables

Also in PHP >=5.3 you could use anonymous functions/closures to pass other data to callback functions.

指尖上的星空 2024-09-06 08:19:25

我在尝试将参数(额外参数)传递给回调时陷入困境
使用 create_function() 和 call_user_function() 方法。

这是供参考的:

<?php
$pattern = "/([MmT][a-z]*)/";
$string = "Mary is a naughty girl because she took all my animals.";
$kill = "Mary";

echo preg_replace_callback($pattern, function($ma) use ($kill) {

    foreach ($ma as $m){
        if ($m == $kill){
            return "Jenny";
        }
        return "($m)";
    }
}, $string);

echo "\n";
?>

$ php preg_replace_callback.php 
Jenny is a naughty girl because she took all (my) ani(mals).

I got stuck while trying to pass an argument (extra parameter) to a callback
with the create_function() and call_user_function() methods.

This is for reference:

<?php
$pattern = "/([MmT][a-z]*)/";
$string = "Mary is a naughty girl because she took all my animals.";
$kill = "Mary";

echo preg_replace_callback($pattern, function($ma) use ($kill) {

    foreach ($ma as $m){
        if ($m == $kill){
            return "Jenny";
        }
        return "($m)";
    }
}, $string);

echo "\n";
?>

$ php preg_replace_callback.php 
Jenny is a naughty girl because she took all (my) ani(mals).
撩动你心 2024-09-06 08:19:25

是的,我使用类似这样的方法来设置和取消设置更改变量,以便回调函数可以使用它,并且您不需要较新的 php 来执行此操作:

foreach ($array as $key) {
    $this->_current_key = $key;
    preg_replace_callback($regex, array($this, '_callback'), $content);
    unset($this->_current_key);
}

然后在回调函数中 $this->_current_key 可用:

function _callback ($match) {    
    //use the key to do something
    new_array[$key] = $match[0];

    //and still remove found string
    return '';
}

yes I use something like this to set and unset a changing variable so that it is available to the callback function and you don't need the newer php to do it:

foreach ($array as $key) {
    $this->_current_key = $key;
    preg_replace_callback($regex, array($this, '_callback'), $content);
    unset($this->_current_key);
}

then in the callback function $this->_current_key is available:

function _callback ($match) {    
    //use the key to do something
    new_array[$key] = $match[0];

    //and still remove found string
    return '';
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文