Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

list all defined constants from a file in php

Tags:

arrays

php

I have some php files that includes some language constants

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

now I need to read each file and list all constants and their values

and to return an output like this :

constant name : value is :

so I think there should be function to list all defined constants of a given php file.

I'm aware of functions like token_get_all or get_defined_constants but i wasn't able to do it.

like image 971
Mac Taylor Avatar asked Sep 24 '11 09:09

Mac Taylor


3 Answers

If the files do contain nothing but define statements, you can use 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);

What it does is basically: get_defined_constants(true) gives us an array of arrays with all available constants, sorted by sections (core, user, ..) - the array under the key 'user' gives us all user-defined constants that we defined in our php code using define, up to that point. array_diff_assoc gives us the difference between this array before and after the file got included.. and that is exactly a list of all constants that got defined in that specific file (as long as there is none of the declarations a duplicate, meaning a constant with that exact name has been defined before - but this would cause an error anyway).

like image 169
Niko Avatar answered Oct 01 '22 22:10

Niko


this is the php script you need:

<?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>";
?>

The result will be something similar to this:

array(5) {
  ["_SEARCH"]=>
  string(6) "Search"
  ["_LOGIN"]=>
  string(5) "Login"
  ["_WRITES"]=>
  string(6) "writes"
  ["_POSTEDON"]=>
  string(9) "Posted on"
  ["_NICKNAME"]=>
  string(8) "Nickname"
}
like image 43
jondinham Avatar answered Oct 01 '22 23:10

jondinham


Late to the game here but I had a similar issue. You could use an include() substitute/wrapper function that logs constants in an accessible global array.

<?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>';

?>

This will result in something like:

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

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

)
like image 38
Eric P Avatar answered Oct 02 '22 00:10

Eric P