首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >preg_replace_callback语法在PHP5.3中工作,但在PHP5.2中不起作用

preg_replace_callback语法在PHP5.3中工作,但在PHP5.2中不起作用
EN

Stack Overflow用户
提问于 2013-08-07 07:45:54
回答 1查看 1.1K关注 0票数 1

下面是一个小的php示例:

代码语言:javascript
复制
echo '<pre>';

// Execute httpd.exe -V to find apache version
exec('"E:\Program Files\AMPPS\apache\bin\httpd.exe" -V', $out, $ret);

// preg_replace_callback to fetch version
echo $apver = preg_replace_callback('/Server version: Apache\/(.*?) \((.*?)\)/is', function ($matches){ return $apache_version = trim($matches[1]); } ,$out[0]);
echo "\n";
echo "\n";

// Test this file with PHP 5.3
exec('"E:\Program Files\AMPPS\php\php.exe" -l "'.__FILE__.'"', $out1, $ret1);
print_r(array($out1, $ret1));

// Test this file with PHP 5.2
exec('"E:\Program Files\AMPPS\php-5.2\php.exe" -l "'.__FILE__.'"', $out2, $ret2);
print_r(array($out2, $ret2));

产出:

代码语言:javascript
复制
2.4.6

Array
(
    [0] => Array
        (
            [0] => No syntax errors detected in E:\Program Files\AMPPS\www\preg_replace.php
        )

    [1] => 0
)
Array
(
    [0] => Array
        (
            [0] => 
            [1] => Parse error: syntax error, unexpected T_FUNCTION in E:\Program Files\AMPPS\www\preg_replace.php on line 5
            [2] => Errors parsing E:\Program Files\AMPPS\www\preg_replace.php
        )

    [1] => -1
)

我需要一个在PHP5.2和5.3中都能使用的语法。

谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-08-07 07:48:50

使用Closures关键字的function定义只有在PHP5.3之前才可用,在5.3版本之前,您只能使用function来定义匿名函数。

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18097537

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档