php 使用“函数(数组 $matches)”时出现意外的 T_FUNCTION 错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3657357/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
unexpected T_FUNCTION error when using "function (array $matches)"
提问by Mike
Hi I'm using the following code but I'm getting an "unexpected T_FUNCTION" syntax error for the second line. Any suggestions?
嗨,我正在使用以下代码,但第二行出现“意外的 T_FUNCTION”语法错误。有什么建议?
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);
回答by BoltClock
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.
当您的 PHP 超过 5.3 时会发生这种情况。匿名函数支持直到 5.3 才可用,因此 PHP 不会识别作为参数传递的函数签名。
You'll have to create a function the traditional way, and pass its name instead (I use link_code()
for example):
您必须以传统方式创建一个函数,并传递其名称(link_code()
例如我使用):
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);
Also, array $matches
is not a problem because type hinting for arrays is supported in PHP 5.2.
此外,array $matches
这不是问题,因为 PHP 5.2 支持数组的类型提示。