duan0414 2017-08-18 23:48
浏览 72
已采纳

带有var_dump的preg_replace_callback()的异常输出

<?php
echo preg_replace_callback('~-([a-z])~', function ($match) {
    //return strtoupper($match[1]);
        var_dump($match);
    }, 'hello-world');
?>

This is a modification on the Example #1 on http://php.net/manual/en/functions.anonymous.php . The var_dump within the anonymous function outputs this:

array(2) { [0]=> string(2) "-w" [1]=> string(1) "w" } helloorld

Anybody has an idea what may be going on?

Thanks.

  • 写回答

2条回答 默认 最新

  • doulian1852 2017-08-19 00:01
    关注

    This should explain the regex part. Now to the echo part where -w is missing: as you can see, preg_replace_callback does operations on $match. Since $match[0] is your matched string, preg_replace_callback expects a replacement as return value in the anonymous function. You have skipped that part in your example, thus, the replacement is empty.

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

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部