使PHP 5.3中的匿名函数与PHP 5.2一起使用 [英] Making anonymous functions from PHP 5.3 work with PHP 5.2
问题描述
我有一个匿名函数,现在我需要对其进行更新以与PHP 5.2兼容.该函数(下面)采用文本并将每个句子的首字母大写.
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;
}
我尝试拉出函数,但是收到一条错误消息,指出回调中的参数2现在丢失了.有关如何解决此问题的任何想法?
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;
}
错误通知:警告:preg_replace_callback() [function.preg-replace-callback]:要求参数2"U S"为 有效的回调
Error notice: Warning: preg_replace_callback() [function.preg-replace-callback]: Requires argument 2, 'U S', to be a valid callback
推荐答案
preg_replace_callback()
作为第二个参数需要一个可调用函数,即函数本身,而不是函数返回的值.
preg_replace_callback()
as a second argument requires a callable, that is a function itself, not a returned value from a function.
所以只需将upper_case($input)
替换为upper_case
,它看起来就像这样
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)));
这篇关于使PHP 5.3中的匿名函数与PHP 5.2一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!