Orlo
Orlo

Reputation: 828

Convert preg_replace to preg_replace_callback

I need help in converting preg_replace to preg_replace_callback.

PHP 5.4 and above firing the following statement: The /e modifier is deprecated, use preg_replace_callback instead in

I've tried changing:

if (stripos ( $tpl->copy_template, "[category=" ) !== false) {
    $tpl->copy_template = preg_replace ( "#\\[category=(.+?)\\](.*?)\[/category\\]#ies",         "check_category('\\1', '\\2', '{$category_id}')", $tpl->copy_template );
}

to

if (stripos ( $tpl->copy_template, "[category=" ) !== false) {
    $tpl->copy_template = preg_replace_callback ( "#\\[category=(.+?)\\](.*?)\\[/category\\]#isu", 
        function($cat){
            return check_category($cat['1'], $cat['2'], $category_id);
        }
    , $tpl->copy_template );
}

the return is empty

Upvotes: 0

Views: 287

Answers (1)

Sharanya Dutta
Sharanya Dutta

Reputation: 4021

Since $category_id is a global variable, you have to use it inside a function with the global keyword. And the keys of a numeric array are integers rather than strings; so you have to write $cat[1] instead of $cat['1'] and $cat[2] instead of $cat['2'].

With these minor alterations, your function becomes:

function($cat){
        global $category_id;
        return check_category($cat[1], $cat[2], $category_id);
}

Upvotes: 2

Related Questions