-1

I have a PHP page in which I am using following command:-

$abc = preg_replace('/&#(\d+);/me', "chr(\\1)", $abc);

Now I want to replace the above command with the "preg_replace_callback" function as preg_replace is deprecated in PHP 5.4 .

How can I achieve it?

0

1 Answer 1

-1

Try using anonymous functions like this :

$abc = preg_replace_callback(
    '/&#(\d+);/me',
    function ($match) 
    { 
        return chr($match[1]);
    },
    $abc
);
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks a lot for the reply. I used the code you specified but I am getting an error in it saying:preg_replace_callback() "Requires argument 2, 'function($a) { return chr($a[1]); }', to be a valid callback"
Not sure why that was happening. Maybe $a is a reserved variable. Try the edited code.
Actually it is giving me error at the function keyword saying incorrect syntax.Do I need to put the entire thing in single quotes?
Not quite. The documentation requires it to be an actual function without quotes. Allow me to add indentation to the code.
Yup I too saw the definition. But I am getting the same issue.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.