问题
I have an anonymous functions that I now need to update to be compatible with PHP 5.2. The function (below) takes text and uppercases the first letter of every sentence.
function clean_text($input) {
$output = $input;
$output = preg_replace_callback('/([.!?])\s*(\w)/', function ($matches) {
return strtoupper($matches[1] . ' ' . $matches[2]);
}, ucfirst(strtolower($input)));
return $output;
}
I tried pulling the function out, but I'm receiving an error stating that argument 2 in the callback is now missing. Any ideas on how to resolve this?
function clean_text($input) {
function upper_case($input) {
return strtoupper($input[1] . ' ' . $input[2]);
}
$output = preg_replace_callback('/([.!?])\s*(\w)/', upper_case($input), ucfirst(strtolower($input)));
return $output;
}
Error notice: Warning: preg_replace_callback() [function.preg-replace-callback]: Requires argument 2, 'U S', to be a valid callback
回答1:
preg_replace_callback()
as a second argument requires a callable, that is a function itself, not a returned value from a function.
So just replace, upper_case($input)
with upper_case
, so it would look like this
preg_replace_callback('/([.!?])\s*(\w)/', 'upper_case', ucfirst(strtolower($input)));
来源:https://stackoverflow.com/questions/24958122/making-anonymous-functions-from-php-5-3-work-with-php-5-2