List all defined constants from file in php

I have some php files that contain 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 the constants and their values

and return this conclusion:

permanent name: value:

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

I know functions like token_get_all or get_defined_constants, but I couldn’t do this.

+6
source share
5 answers

If the files have 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); 

Basically this: get_defined_constants(true) gives us an array of arrays with all available constants sorted by sections (kernel, user, ..) - the array under the "user" key gives us all the user constants that we defined in our php code using define , up to this point. array_diff_assoc gives us the difference between this array before and after the file was included .. and this is exactly a list of all the constants that were defined in this particular file (as long as none of the declarations are duplicated, which means a constant with this exact name has been determined before - but it will still cause an error).

+7
source

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 like this:

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

Late for the game here, but I had a similar problem. You can use the include () substitute / wrapper function, which registers 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
         )

 )
+1
source

Assuming you want to do this at run time, you should take a look at PHP Reflection , specifically ReflectionClass :: getConstants () , which allows you to do exactly what you think.

0
source

I had the same problem too. I went from jondinham's suggestion, but I prefer to use regex because it is a little easier to control and flexible. Here is my version of the solution:

 $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); 
-1
source

Source: https://habr.com/ru/post/897957/


All Articles