0

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

2
  • What does check_category do? Commented Feb 4, 2014 at 2:46
  • checks for categories... it quite big, so i decided not to include it. function check_category($cats, $block, $category, $action = true) {..} Commented Feb 4, 2014 at 2:53

1 Answer 1

2

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);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.