使用“函数(数组 $matches)"时出现意外的 T_FUNCTION 错误; [英] unexpected T_FUNCTION error when using "function (array $matches)"

查看:38
本文介绍了使用“函数(数组 $matches)"时出现意外的 T_FUNCTION 错误;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码,但第二行出现意外的 T_FUNCTION"语法错误.有什么建议吗?

Hi I'm using the following code but I'm getting an "unexpected T_FUNCTION" syntax error for the second line. Any suggestions?

preg_replace_callback("/\\[LINK\=(.*?)\\\](.*?)\\[\/LINK\\]/is",
function (array $matches) {
    if (filter_var($matches[1], FILTER_VALIDATE_URL))
        return '<a href="'.
            htmlspecialchars($matches[1], ENT_QUOTES).
            '" target="_blank">'.
            htmlspecialchars($matches[2])."</a>";
    else
        return "INVALID MARKUP";
}, $text);

推荐答案

当您的 PHP 低于 5.3 时会发生这种情况.匿名函数支持直到 5.3 才可用,因此 PHP 不会识别作为参数传递的函数签名.

That happens when your PHP is older than 5.3. Anonymous function support wasn't available until 5.3, so PHP won't recognize function signatures passed as parameters like that.

您必须以传统方式创建一个函数,并改为传递其名称(例如,我使用 link_code()):

You'll have to create a function the traditional way, and pass its name instead (I use link_code() for example):

function link_code(array $matches) {
    if (filter_var($matches[1], FILTER_VALIDATE_URL))
        return '<a href="'.
            htmlspecialchars($matches[1], ENT_QUOTES).
            '" target="_blank">'.
            htmlspecialchars($matches[2])."</a>";
    else
        return "INVALID MARKUP";
}

preg_replace_callback("/\\[LINK\=(.*?)\\\](.*?)\\[\/LINK\\]/is", 'link_code', $text);

此外,array $matches 不是问题,因为 PHP 5.2 支持数组的类型提示.

Also, array $matches is not a problem because type hinting for arrays is supported in PHP 5.2.

这篇关于使用“函数(数组 $matches)"时出现意外的 T_FUNCTION 错误;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆