列出php中文件中所有已定义的常量

时间:2011-09-24 09:04:57

标签: php arrays

我有一些包含一些语言常量的php文件

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

现在我需要读取每个文件并列出所有常量及其值

并返回如下输出:

常量名称: 价值是:

所以我认为应该有一个函数来列出给定php文件的所有已定义的常量。

我知道像token_get_all或get_defined_constants这样的函数,但我无法做到。

5 个答案:

答案 0 :(得分: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)为我们提供了一个包含所有可用常量的数组数组,按部分排序(核心,用户,...) - 关键字'user'下的数组为我们提供了所有用户 - 我们使用define在我们的PHP代码中定义的定义常量,直到那时为止。 array_diff_assoc告诉我们在包含文件之前和之后这个数组之间的区别..这正是在该特定文件中定义的所有常量的列表(只要没有重复的声明,意思是之前已经定义了具有该确切名称的常量 - 但这无论如何都会导致错误。)

答案 1 :(得分: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"
}

答案 2 :(得分:1)

这里的游戏很晚,但我遇到了类似的问题。您可以使用include()替换/包装函数将常量记录在可访问的全局数组中。

delay

这将导致类似:

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

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

)

答案 3 :(得分:0)

假设您想在运行时执行此操作,您应该查看PHP Reflection,特别是ReflectionClass::getConstants(),这样可以让您完全按照自己的意愿行事。

答案 4 :(得分:-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);
相关问题