Making anonymous functions from PHP 5.3 work with PHP 5.2

扶醉桌前 提交于 2019-12-04 06:42:27

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!