-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?

Gaurang
  • 371
  • 2
  • 4
  • 21

1 Answers1

-1

Try using anonymous functions like this :

$abc = preg_replace_callback(
    '/&#(\d+);/me',
    function ($match) 
    { 
        return chr($match[1]);
    },
    $abc
);
EnKrypt
  • 777
  • 8
  • 23
  • 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" – Gaurang Oct 21 '14 at 11:02
  • Not sure why that was happening. Maybe $a is a reserved variable. Try the edited code. – EnKrypt Oct 21 '14 at 11:15
  • Actually it is giving me error at the function keyword saying incorrect syntax.Do I need to put the entire thing in single quotes? – Gaurang Oct 21 '14 at 11:16
  • Not quite. [The documentation](http://php.net/manual/en/function.preg-replace-callback.php) requires it to be an actual function without quotes. Allow me to add indentation to the code. – EnKrypt Oct 21 '14 at 11:18
  • Yup I too saw the definition. But I am getting the same issue. – Gaurang Oct 21 '14 at 11:40
  • Hey I sorted out the above mentioned issue. But now I am getting the error : "Modifier /e cannot be used with replacement callback" – Gaurang Oct 21 '14 at 12:05