简体   繁体   English

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

[英]Is there a way to pass another parameter in the preg_replace_callback callback function?

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. 因此,php手册说在preg_replace_callback()使用类方法作为回调,该方法必须是抽象的。

I need to pass an instance of a previuosly initialized object (in the example, an instance of the Something class) at the callback function. 我需要在回调函数中传递一个previuosly初始化对象的实例(在该示例中, Something类的实例)。

I tryed to use call_user_func() , but doesnt work (becose in this way i miss the matches parameter). 我尝试使用call_user_func() ,但不起作用(因为这样我错过了matches参数)。

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 )? 有没有办法做到这一点,或者让我分开进程(在preg_match_all之前做,为每个匹配检索替换值,然后是一个简单的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): 编辑:作为旁注,在tom haigh回答之前,我使用了这个解决方法(在这个例子中,这是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;
}

Hope can help someone. 希望可以帮助别人。

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: 你可以使Bar()不是静态的,并使用它:

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

Then the callback function will be able to see $this 然后回调函数将能够看到$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. 同样在PHP> = 5.3中,您可以使用匿名函数/闭包将其他数据传递给回调函数。

I got stuck while trying to pass an argument (extra parameter) to a callback with the create_function() and call_user_function() methods. 我试图通过create_function()和call_user_function()方法将参数(额外参数)传递给回调时遇到困难。

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

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: 是的我使用这样的东西设置和取消设置一个变化的变量,以便它可用于回调函数,你不需要更新的PHP来做它:

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: 然后在回调函数$ this - > _ current_key可用:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM