0

Latest Edit:
Well, I came up with fairly "reliable" :) solution in form of a (portable) function, but since some ppl here got irked enough by not understanding the problem and blocked this question (a military solution: kill what you do not understand), I cannot post it here. Pity.

I have a set of files, which contain constants, like below.

define ('LNG_GSU_LNK_LBL',  '[details]');
define( 'LNG_METHODCROSS_GSU_CLS'   ,'class');
define('GSU_METH'  ,  'method');
define ( 'CROSS_GSU_ACTION_NO_REMOVE', 'cannot remove \' module \'(is); deployed');

What would be most reliable method to retrieve constant names and values from given, selected file.

EDIT:

I need to get these constants into array, without defining them actually, directly by reading file, e.g.:

array('LNG_GSU_LNK_LBL'=>'[details]','LNG_METHODCROSS_GSU_CLS'=> 'class') 

... etc

EDIT 2: So far I got this far:

$file_array = file($path, FILE_SKIP_EMPTY_LINES);

//implode lang file into a string removing php tags
$string1 = implode('', $file_array);
$string2 = str_replace(array(''), '', $string1);

//regex removing content between markers
$regex = '/\/\*.+?\*\//si';
$replace_with = '';
$replace_where = $string2;
$string3 = preg_replace($regex, $replace_with, $replace_where);

//regex: remove multiple newlines
$string4 = preg_replace("/\n+/", "\n", $string3);

EDIT 3:

expected result

array (
'LNG_GSU_LNK_LBL' => '[details]',
'LNG_METHODCROSS_GSU_CLS' => 'class',
'GSU_METH' => 'method',
'CROSS_GSU_ACTION_NO_REMOVE' => 'cannot remove \' module \'(is); deployed'
);
Jeffz
  • 2,075
  • 5
  • 36
  • 51
  • 4
    just include\require it. –  Nov 17 '14 at 22:11
  • 1
    Uh.......what? Reliable? – Jay Blanchard Nov 17 '14 at 22:11
  • 2
    no one ever asks for an un-relaibel way to do something –  Nov 17 '14 at 22:13
  • As you tagged regex, are you asking how to retrieve the names and values without actually running the code and defining the constants? – Steve Nov 17 '14 at 22:15
  • I do not know constant names, so including requiring will not help. Preferably, I would like to get them into an array with key:name, val:value. Also since structure is irregular, there is a problem wit regex - hence "reliable". – Jeffz Nov 17 '14 at 22:16
  • 2
    if you don't know the constants name, there's something wrong with your approach - what's the big picture here? –  Nov 17 '14 at 22:16
  • @Jay Blanchard word "reliable" had to do with regex, since structure is irregular, I'm having difficulty formulating "reliable" regex; – Jeffz Nov 17 '14 at 22:21
  • Wow, there's a lot of stuff that you haven't told us! I'll bet you didn't expect the Spanish Inquisition! Let's go out and come in again... – Jay Blanchard Nov 17 '14 at 22:21
  • @Steve: yes, preferably into an array; – Jeffz Nov 17 '14 at 22:22
  • If you don't know the names and they are not included/required how are you planning to get to them? – Jay Blanchard Nov 17 '14 at 22:23
  • @Jay Blanchard by reading file directly I just have problem with "reliable" regex – Jeffz Nov 17 '14 at 22:28
  • I think he doesn't want to use the constants, but rather only get their names and their values, as they come "in a set of files". Something like `preg_match_all("#define\('([-_\w]+)','(.+)');#Ui", $texttoreplace, $matches); // then play with $matches` – Benjamin C. Nov 17 '14 at 22:28
  • Why? What problem are you trying to solve? – DanMan Nov 17 '14 at 22:47
  • @DanMan I'm trying to put constants from selected file (only!) into an array (preferably) – Jeffz Nov 17 '14 at 23:21

2 Answers2

2

If you dont want to include the file, then you should use: token_get_all().

Otherwise, you should require/include the file containing them and you can iteratively use get_defined_constants():

$all = array();

$consts = get_defined_constants();
foreach($consts as $k=>$v){
   if (strpos($k,"LNG")===0 && !isset($all[$k]))    
      $all[$k]=$v;
}

Note that parsing php source code is like parsing HTML with regex, better bet avoid it.

Community
  • 1
  • 1
dynamic
  • 46,985
  • 55
  • 154
  • 231
  • There are dozens of files with constants. There are thousands of constants in them, each of them starts with "LNG". Plus I need to get constants from given (selected) file only. – Jeffz Nov 17 '14 at 22:55
  • What's the problem to include selected only files? you can do it in a `foreach() {include()}` – dynamic Nov 17 '14 at 22:56
  • Problem is that at the given time (when selected file is processed/included) other files with LNG constants can be included and constants from these files will be picked up by get_defined_constants(), so it will not work, as I need only constants from selected file. – Jeffz Nov 17 '14 at 23:20
  • To clarify: constants not necessarily start with LNG_ - some do not. – Jeffz Nov 19 '14 at 14:35
1

Building on dynamic's answer, include the file within another, separate, web accessible file, that is not loaded within your current application (so will have no other user defined constants at run time):

//standalone.php
include "that_file.php";

$consts = get_defined_constants(true);
$newUserConsts = $consts['user'];
echo json_encode($newUserConsts);

//within your application

$newUserConsts = json_decode(file_get_contents('http://yoursite.com/standalone.php'));

Or if you cant make a separate web accessible file:

$consts = get_defined_constants(true);
$existingUserConsts = $consts['user'];

include "that_file.php";

$consts = get_defined_constants(true);
$newUserConsts = $consts['user'];

var_dump(array_diff_key($newUserConsts, $existingUserConsts));
Steve
  • 20,703
  • 5
  • 41
  • 67
  • I tried this idea (array_diff) but there were problems with streamlining it .. thank you for your time thou – Jeffz Nov 19 '14 at 16:31