2013-08-07 31 views
1

這裏是PHP小例子:preg_replace_callback語法工作在PHP 5.3,但不是在PHP 5.2

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)); 

輸出:

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 
) 

我需要一種語法,將同時在PHP 5.2和5.3工作。

謝謝。

回答

5

Closures定義使用function關鍵字只有自PHP 5.3開始纔可用在5.3版本之前,您只能使用create_function來定義匿名函數。

+0

完成。我會在外面創建一個回調函數。並在參數中指定其名稱。無法儘快接受答案。呵呵。 – Jigar

+0

你可以改變你的第二個鏈接從/ ru /'到'/ en /'請:) – Emissary

+0

oops。代理錯誤,謝謝。這是「智能谷歌」替代外部IP位置語言:頁碼 –