-3

Error: Depreciated

 if( preg_match('/([^:]+): (.+)/m', $field, $match) ) {
            $match[1] = preg_replace_callback('/(?<=^|[\x09\x20\x2D])./e', 'strtoupper("\0")', strtolower(trim($match[1])));

How to use preg_replace_callback instead of preg_replace ? I tried to replace the preg_replace and got another error.

PHP Warning: preg_replace_callback(): Requires argument 2, 'strtoupper("\0")', to be a valid callback in

mega6382
  • 9,211
  • 17
  • 48
  • 69
azarudeen
  • 125
  • 3
  • 16

3 Answers3

1

preg_replace_callback() should specify a exact callback. Check more about preg_replace_callback() here: preg_replace_callback

if( preg_match('/([^:]+): (.+)/m', $field, $match) ) {
            $match[1] = preg_replace_callback('/(?<=^|[\x09\x20\x2D])./e', function ($matches) {
            return strtoupper("\0");
        }, strtolower(trim($match[1])));
スージン
  • 143
  • 8
  • Thanks, Got another error message : PHP Warning: preg_replace_callback(): Modifier /e cannot be used with replacement callback in – azarudeen Oct 24 '17 at 06:00
0

You need to use an actual callback. Try the following:

 if( preg_match('/([^:]+): (.+)/m', $field, $match) ) {
            $match[1] = preg_replace_callback('/(?<=^|[\x09\x20\x2D])./e', function($matches){return strtoupper("\0");}, strtolower(trim($match[1])));

A callback can be either an anonymous function like above or a name of a function as a string.

Note: \e modifier was DEPRECATED in , and REMOVED as of , you must use preg_replace_callback, anywhere where you would use \e. learn more about it here. Can someone explain the /e regex modifier?

mega6382
  • 9,211
  • 17
  • 48
  • 69
  • Thanks, Got another error message : PHP Warning: preg_replace_callback(): Modifier /e cannot be used with replacement callback in – azarudeen Oct 24 '17 at 06:01
  • Just remove the `/e` from your regex. You are using `preg_replace_callback()`, so there is no need for that. – mega6382 Oct 24 '17 at 06:02
  • 1
    `\e` is deprecated, learn more about it here. https://stackoverflow.com/questions/16986331/can-someone-explain-the-e-regex-modifier – mega6382 Oct 24 '17 at 06:04
0

Signature of preg_replace_callback from the PHP documentation:

mixed preg_replace ( mixed $pattern , mixed $replacement , mixed $subject [, int $limit = -1 [, int &$count ]] )

You need to pass a callback function as the second argument.

  $html = $_POST['html'];

  // uppercase headings
  $html = preg_replace_callback(
    '(<h([1-6])>(.*?)</h\1>)',
    function ($m) {
        return "<h$m[1]>" . strtoupper($m[2]) . "</h$m[1]>";
    },
    $html
  );
regulus
  • 1,572
  • 12
  • 18