代码之家  ›  专栏  ›  技术社区  ›  Strae

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

  •  9
  • Strae  · 技术社区  · 16 年前

    伙计们,我真的希望我的英语能很好地解释我需要什么。

    让我们拿着这个 例子 (这只是一个例子!)代码:

    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 ,对于每个匹配项,检索replace值,然后 preg_replace )?

    编辑: 另外,在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;
    }
    

    希望能帮助别人。

    3 回复  |  直到 13 年前
        1
  •  13
  •   Tom Haigh    16 年前

    很难将参数传递给回调,但不这样做:

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

    你可以 Bar() 不是静态的,使用这个:

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

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

    请参阅中的“回调” Pseudo-types and variables

    在php>=5.3中也可以使用 anonymous functions/closures 将其他数据传递给回调函数。

        2
  •  12
  •   Ярослав Рахматуллин    13 年前

    我在试图将参数(额外参数)传递给回调时卡住了 使用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).
    
        3
  •  0
  •   René Höhle oasisfleeting    13 年前

    是的,我使用这样的方法来设置和取消设置一个变化的变量,以便回调函数可以使用它,而不需要更新的php来执行此操作:

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

    然后在回调函数$this->\u current\u键可用:

    function _callback ($match) {    
        //use the key to do something
        new_array[$key] = $match[0];
    
        //and still remove found string
        return '';
    }
    
    推荐文章