首页 > 解决方案 > PHP 已弃用:函数 create_function()

问题描述

我越来越不推荐使用 PHP:函数 create_function()

使用以下代码:

function fetch_sentence_case($text)
{
    return preg_replace_callback(
        '#(^|\.\s+|\:\s+|\!\s+|\?\s+)[a-z]#',
        create_function('$matches', 'return strtoupper($matches[0]);'),
        vbstrtolower($text)
    );
}

我试过了

function fetch_sentence_case($text)
{
    return preg_replace_callback(
        '#(^|\.\s+|\:\s+|\!\s+|\?\s+)[a-z]#',
        function($matches) { return strtoupper($matches[0]; },
        vbstrtolower($text)
    );
}

但它不起作用。

???

标签: phpfunctiondeprecatedcreate-function

解决方案


我确实意识到您可能由于 PHP 版本而受限于这种方法,但请查看箭头函数

Wadih 上面的评论是正确的。你缺少括号。


推荐阅读