dougaodi8895 2012-09-13 09:50
浏览 71
已采纳

php:为什么preg_replace_callback不允许内置php函数,只是匿名用户函数?

This does not work, and outputs an empty string:

$check["pattern"] = "correct";
$text = "Could this be correct?";
echo preg_replace_callback($check["pattern"],ucfirst,$text);

Would have been nice to use built in functions. In fact general callbacks do allow built in functions as per http://php.net/manual/en/language.types.callable.php, but not preg_replace_callback. Could be a feature request for php?

  • 写回答

2条回答 默认 最新

  • douqinlin5094 2012-09-13 09:58
    关注

    Your code should trigger a notice and a warning:

    • Notice: Use of undefined constant ucfirst - assumed 'ucfirst'
    • Warning: preg_replace_callback(): Delimiter must not be alphanumeric or backslash

    If it doesn't, you seriously need to check your PHP error reporting settings. Fixing the code with the help of the error messages:

    $check["pattern"] = "/correct/";
    $text = "Could this be correct?";
    echo preg_replace_callback($check["pattern"],'ucfirst',$text);
    

    ... we get this:

    Warning: ucfirst() expects parameter 1 to be string, array given

    So using a builtin callback function is working fine. However, as the manual page for ucfirst() explains, the function expects a string, not an array. And, as the manual page for preg_replace_callback() explains:

    A callback that will be called and passed an array of matched elements in the subject string.

    To sum up: it isn't a sensible feature request, it's a bug in your code ;-)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用