-2

I have a string that I need help converting to preg_replace_callback. Any help with an explanation would be helpful.

Thanks

preg_replace('/(?<=^|[\x09\x20\x2D])./e', 'strtoupper("\0")', strtolower(trim($match[1])));
puks1978
  • 3,667
  • 11
  • 44
  • 103

1 Answers1

0

Here is exact example from the manual with small change:

$string = preg_replace_callback(
        '/(?<=^|[\x09\x20\x2D])./',
        create_function(
            // single quotes are essential here,
            // or alternative escape all $ as \$
            '$matches',
            'return strtoupper($matches[0]);'
        ),
        $string
    );

or:

function myfunc($matches)
{
  return strtoupper($matches[0]);
}
$string = preg_replace_callback("/(?<=^|[\x09\x20\x2D])./", "myfunc", $string);
  • 1
    Replacing the `/e` modifier with `create_function` is pretty counter-productive, as both are essentially using `eval` underneath, with all the problems that entails. Use a real anonymous function, unless for some reason you need to simultaneously support PHP < 5.3 *and* some future version of PHP which doesn't have the `/e` modifier. – IMSoP Aug 29 '13 at 23:40