使 PHP 5.3 中的匿名函数与 PHP 5.2 一起使用 [英] Making anonymous functions from PHP 5.3 work with PHP 5.2
问题描述
我有一个匿名函数,我现在需要更新它以与 PHP 5.2 兼容.该函数(如下)将文本和每个句子的第一个字母大写.
function clean_text($input) {$输出= $输入;$output = preg_replace_callback('/([.!?])\s*(\w)/', function ($matches) {返回 strtoupper($matches[1] . ' ' . $matches[2]);}, ucfirst(strtolower($input)));返回 $output;}
我尝试拉出该函数,但我收到一条错误消息,指出现在缺少回调中的参数 2.有关如何解决此问题的任何想法?
function clean_text($input) {函数大写($输入){返回 strtoupper($input[1] . ' ' . $input[2]);}$output = preg_replace_callback('/([.!?])\s*(\w)/', upper_case($input), ucfirst(strtolower($input)));返回 $output;}
<块引用>
错误提示:警告:preg_replace_callback()[function.preg-replace-callback]: 需要参数 2, 'U S', 是一个有效回调
preg_replace_callback()
作为第二个参数需要一个可调用对象,即函数本身,而不是函数的返回值.
所以只需将 upper_case($input)
替换为 upper_case
,它看起来像这样
preg_replace_callback('/([.!?])\s*(\w)/', 'upper_case', ucfirst(strtolower($input)));
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
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)));
这篇关于使 PHP 5.3 中的匿名函数与 PHP 5.2 一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!