Search code examples
preg-replacepreg-replace-callback

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead


I need a little help. Because preg_replace is deprecated, I have to convert all my preg_replace to preg_replace_callback...

function parse_bb_tpl ($part, $args)
{
    // just a replace, with evaluation...
    return preg_replace (
        '/{([^}\s]+)}/e',
        "isset (\$args['\\1']) ? \$args['\\1'] : '';",
        $this->_tpls[$part]
    );
}

Solution

  • Casimir is correct ... it is the e modifier that is being depricated. preg_replace_callback is pretty cool, though. Basically, you just make a function that gets fed the matches and use that for doing the evaluation.

    $string = 'The bat is batty in the bathroom';
    
    $string = preg_replace_callback('/bat/', 'cool_function', $string);
    
    print $string;
    
    function cool_function($matches) {
        return '<b>'.$matches[0].'</b>';    
    }
    

    That outputs this:

    The <b>bat</b> is <b>bat</b>ty in the <b>bat</b>hroom