jdneill Posted February 12, 2009 Share Posted February 12, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/144915-getting-array-from-the-php-define-function-using-its-arguments/ Share on other sites More sharing options...
.josh Posted February 12, 2009 Share Posted February 12, 2009 <?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. Quote Link to comment https://forums.phpfreaks.com/topic/144915-getting-array-from-the-php-define-function-using-its-arguments/#findComment-760617 Share on other sites More sharing options...
jdneill Posted February 12, 2009 Author Share Posted February 12, 2009 <?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 Quote Link to comment https://forums.phpfreaks.com/topic/144915-getting-array-from-the-php-define-function-using-its-arguments/#findComment-760630 Share on other sites More sharing options...
PFMaBiSmAd Posted February 12, 2009 Share Posted February 12, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/144915-getting-array-from-the-php-define-function-using-its-arguments/#findComment-760639 Share on other sites More sharing options...
.josh Posted February 12, 2009 Share Posted February 12, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/144915-getting-array-from-the-php-define-function-using-its-arguments/#findComment-760645 Share on other sites More sharing options...
nrg_alpha Posted February 12, 2009 Share Posted February 12, 2009 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 ) Quote Link to comment https://forums.phpfreaks.com/topic/144915-getting-array-from-the-php-define-function-using-its-arguments/#findComment-760652 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.