Jump to content

Getting array from the php 'define' function using its arguments


jdneill

Recommended Posts

I have a script that loads a file contents into a string and removes all the contents expect the php function 'define. I want to be able to extract the 1st and second argument of this function into an array to be edited elsewhere. Is there a way of doing this using regx?

 

Example

 

$string = "
define( 'CONTSTANT1', 'Text for constant one' );
define( 'CONTSTANT2', 'Text for constant two' ); 
define( 'CONTSTANT3', 'Text for constant three' );
define( 'CONTSTANT4', 4 );
";

 

Turn the above into an array like:

 

$stringArray[] = array(

'CONTSTANT1' => 'Text for constant one',

'CONTSTANT2' => 'Text for constant two',

'CONTSTANT3' => 'Text for constant three',

'CONTSTANT4' => 4 );

 

Many thanks in advance,

 

John

Link to comment
Share on other sites

<?php
$string = "
define( 'CONTSTANT1', 'Text for constant one' );
define( 'CONTSTANT2', 'Text for constant two' );
define( 'CONTSTANT3', 'Text for constant three' );
define( 'CONTSTANT4', 4 );
";

preg_match_all("~define\(\s'(.*?)',\s'?(.*?)'?\s\);~s",$string,$matches);
foreach($matches[1] as $k => $v) {
   $stringArray[$v] = $matches[2][$k];
}
?>

 

There is a limitation to this:  if the data that is supposed to be the key or value happens to contain something that matches the regex pattern, it's going to break the regex and return funky results.  No matter how you try to break it down, I don't think there's really a way to prevent that from happening, other than storing your data differently to begin with.

Link to comment
Share on other sites

<?php
$string = "
define( 'CONTSTANT1', 'Text for constant one' );
define( 'CONTSTANT2', 'Text for constant two' );
define( 'CONTSTANT3', 'Text for constant three' );
define( 'CONTSTANT4', 4 );
";

preg_match_all("~define\(\s'(.*?)',\s'?(.*?)'?\s\);~s",$string,$matches);
foreach($matches[1] as $k => $v) {
   $stringArray[$v] = $matches[2][$k];
}
?>

 

There is a limitation to this:  if the data that is supposed to be the key or value happens to contain something that matches the regex pattern, it's going to break the regex and return funky results.  No matter how you try to break it down, I don't think there's really a way to prevent that from happening, other than storing your data differently to begin with.

 

Many thanks,

 

I appreciate you taking the time to do this and your above example has a slight mistake and once I adjusted for this, it worked as expected and without any issues (that I can see just now).

 

This is the code with the slight adjustment:

 

preg_match_all( "~define\(\s'(.*?)'\s,\s'?(.*?)'?\s\);~s", $string, $matches );

 

Thanks,

 

John

Link to comment
Share on other sites

If you are doing this to create a way of editing a configuration file settings, it is much easier to just include the settings file so that the constants become defined and then use $arr = get_defined_constants(true); to get an array $arr['user'] that contains the keys and values so that you can edit them.

Link to comment
Share on other sites

I appreciate you taking the time to do this and your above example has a slight mistake and once I adjusted for this, it worked as expected and without any issues (that I can see just now).

 

I wouldn't call it a mistake if the data you provided didn't show a space before the comma, but I'm glad you nonetheless got it sorted it out.

Link to comment
Share on other sites

Alternatively, to resort your keys / arrays, you can use array_combine:

preg_match_all("~define\(\s'(.*?)',\s'?(.*?)'?\s\);~s",$string,$matches);
$arr = array_combine($matches[1], $matches[2]);
echo '<pre>'.print_r($arr, true);

 

Output:

Array
(
    [CONTSTANT1] => Text for constant one
    [CONTSTANT2] => Text for constant two
    [CONTSTANT3] => Text for constant three
    [CONTSTANT4] => 4
)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.