2011-09-24 22 views
6

我有,其中包括一些語言的一些PHP文件常量列表從一個文件中的所有定義的常量在PHP

define("_SEARCH","Search"); 
define("_LOGIN","Login"); 
define("_WRITES","writes"); 
define("_POSTEDON","Posted on"); 
define("_NICKNAME","Nickname"); 

現在我需要讀取每個文件,並列出所有的常量和它們的值

,並返回像這樣的輸出:

常量名: 值:

,所以我覺得應該有功能列出所有定義的常量給定的php文件。

我知道像token_get_all或get_defined_constants這樣的函數,但我無法做到這一點。

+0

如果您使用'get_defined_constants()',那什麼不起作用? – Niko

+1

我注意到我已閱讀這些手冊,我在這裏關於獲取(特定PHP文件)的所有定義的常量不是所有定義的系統常量 –

+0

好吧,現在我明白了。這些文件中是否有其他PHP代碼/內容? – Niko

回答

2

這是你需要的PHP腳本:

<?php 
//remove comments 
$Text = php_strip_whitespace("your_constants_file.php"); 
$Text = str_replace("<?php","",$Text); 
$Text = str_replace("<?","",$Text); 
$Text = str_replace("?>","",$Text); 
$Lines = explode(";",$Text); 
$Constants = array(); 

//extract constants from php code 
foreach ($Lines as $Line) {  

    //skip blank lines 
    if (strlen(trim($Line))==0) continue; 
    $Line = trim($Line); 

    //skip non-definition lines 
    if (strpos($Line,"define(")!==0) continue; 
    $Line = str_replace("define(\"","",$Line); 

    //get definition name & value 
    $Pos = strpos($Line,"\",\""); 
    $Left = substr($Line,0,$Pos); 
    $Right = substr($Line,$Pos+3); 
    $Right = str_replace("\")","",$Right); 

    $Constants[$Left] = $Right; 
} 

echo "<pre>"; 
var_dump($Constants); 
echo "</pre>"; 
?> 

結果將與此類似:

array(5) { 
    ["_SEARCH"]=> 
    string(6) "Search" 
    ["_LOGIN"]=> 
    string(5) "Login" 
    ["_WRITES"]=> 
    string(6) "writes" 
    ["_POSTEDON"]=> 
    string(9) "Posted on" 
    ["_NICKNAME"]=> 
    string(8) "Nickname" 
} 
+1

它是一個不錯的方法,但我們可以str_replace所有評論像/ *評論* /和裏面有什麼? –

+0

我已經添加了一些代碼行來移除所有評論 – jondinham

7

如果文件包含什麼,但define語句,你可以使用get_defined_constants

function getUserDefinedConstants() { 
    $constants = get_defined_constants(true); 
    return (isset($constants['user']) ? $constants['user'] : array()); 
} 

$constantsBeforeInclude = getUserDefinedConstants(); 
include('file.php'); 
$constantsAfterInclude = getUserDefinedConstants(); 

$newConstants = array_diff_assoc($constantsAfterInclude, $constantsBeforeInclude); 

它所做的基本上是:get_defined_constants(true)爲我們提供了一個數組,其中包含所有可用常量,按部分排序(core,user,..) - 關鍵字'user'下的數組爲我們提供了所有用戶定義的常量,我們在我們的php代碼中使用define到那一點。 array_diff_assoc在文件被包含之前和之後給了我們這個數組之間的區別..這正是所有在那個特定文件中定義的常量的列表(只要沒有任何聲明是重複的,那個確切的名字已經被定義過 - 但是這會導致錯誤)。

+1

但是如何知道php文件的前後常量是「user」。請解釋更多關於使用這個 –

+0

是的,我同意Mac,假設我們正在使用一個框架和一個已包含文件中的常量,我們不能'排除'文件並'重新包含' – jondinham

+0

因爲這些文件是應該是語言文件,我懷疑他們之前包括在內。所以沒有問題。 @MacTaylor'user'沒有單個常量的名稱,它是常量所屬的部分(意味着它們是「user」,而不是php本身)。 '$ constants ['user']'是你在代碼中通過'define()'定義的所有常量的數組。 – Niko

1

在這裏比賽遲到了,但我有一個類似的問題。您可以使用include()替換/包裝函數將常量記錄在可訪問的全局數組中。

<?php 

function include_build_page_constants($file) { 
    global $page_constants ; 
    $before = get_defined_constants(true); 
    include_once($file); 
    $after = get_defined_constants(true); 
    if (isset($after['user'])) { 
     if (isset($before['user'])) { 
      $current = array_diff_assoc($after['user'],$before['user']); 
     }else{ 
      $current = $after['user']; 
     } 
     $page_constants[basename($file)]=$current; 
    } 
} 

include_and_build_page_constants('page1.php'); 
include_and_build_page_constants('page2.php'); 

// test the array 
echo '<pre>'.print_r($page_constants,true).'</pre>'; 

?> 

這將導致類似:

 
Array 
(
    [page1.php] => Array 
     (
      [_SEARCH] => Search 
      [_LOGIN] => Login 
      [_WRITES] => writes 
      [_POSTEDON] => Posted on 
      [_NICKNAME] => Nickname 
     ) 

    [page2.php] => Array 
     (
      [_THIS] => Foo 
      [_THAT] => Bar 
     ) 

) 
-1

我也有同樣的問題。我從jondinham的建議,但我更喜歡使用正則表達式,因爲它更容易控制和靈活。這是我的解決方案版本:

$text = php_strip_whitespace($fileWithConstants); 
$text = str_replace(array('<?php', '<?', '?>'), '', $text); 
$lines = explode(";", $text); 
$constants = array(); 

//extract constants from php code 
foreach ($lines as $line) { 
    //skip blank lines 
    if (strlen(trim($line)) == 0) 
     continue; 

    preg_match('/^define\((\'.*\'|".*"),()?(.*)\)$/', trim($line), $matches, PREG_OFFSET_CAPTURE); 

    if ($matches) { 
     $constantName = substr($matches[1][0], 1, strlen($matches[1][0]) - 2); 
     $constantValue = $matches[3][0]; 
     $constants[$constantName] = $constantValue; 
    } 
} 
print_r($constants); 
相關問題