I am new to PHP and regular expression. I was going thorugh some online examples and came with this example:
<?php
echo preg_replace_callback('~-([a-z])~', function ($match) {
return strtoupper($match[1]);
}, 'hello-world');
// outputs helloWorld
?>
in php.net but to my surprise it does not work and keep getting error:
PHP Parse error: parse error, unexpected T_FUNCTION
Why get error ?
You are using PHP’s Anonymous functions: functions that have no name.
When I run your program I get no error. May be you are trying it on a
PHP < 5.3.Anonymous functions are available since PHP 5.3.0.
If
PHPversion is creating the problem you can re-write the program to not use Anonymous functions as: