0

I'm writing a config editor. I'm reading in the file with fopen and fgets line by line. I want to get the array keys and values from the string. I'm not familiar with regex thats why i'm asking.

Example config file:

$conf['something'] = 'value';
$conf['other_thing']['another_key'] = 3000;

I can't loop through the $conf array, as it is a SuperObject in the system, and holds many additional datas, even inited classes...

3
  • 2
    as you have a php array why you to to read it again using fopen just include the file and use $conf variable Commented Nov 23, 2012 at 14:21
  • why you making your life harder than it could be? Write your config file in more parseable format. Commented Nov 23, 2012 at 14:22
  • Why to make the app slower with adding extra parsing? In my case parsing is only needed when config editor is opened in the admin area. Commented Nov 23, 2012 at 14:40

5 Answers 5

2

I would do smth like this:

// get config file as a variable
require_once('config_file.php');

// get keys
$keys = array_keys($conf);

// get config file as a variable
$values = array_values($conf);
Sign up to request clarification or add additional context in comments.

5 Comments

If you need to scope that $config variable so that it doesn't infect your actual config variable, then you could wrap a function around the include.
I'm not sure I'm following. Can you please add an example?
class configEditor() { private $conf; function load_config() { include 'config_file.php'; $this->conf = $config;
It's hard to write in a comment, I was just imagining a case where loading in the config_file.php may override an existing $config variable. You could avert that by loading in a function scope, a little like the code above.
Tanks, I get it. I was a bit confused as English is not my native and I didn't understand the 'scope' :)
1

While not direct answer to your question but as you wrote "I'm writing a config editor." then I assume you also write this config file down. So assuming your editor keeps config in $conf array while it works, I'd just dump it as JSON instead of your PHP code:

file_put_contents('config.json', json_encode($conf));

and then read it back when needed

$conf = json_decode( file_get_contents('config.json') );

Comments

0

\$conf\['(.*?)']\s+=\s+'(.*?)'; here is your pattern. Key is in the group 1 and value is in the group 2.

$, [ and ] are special characters that's why you should escape them. \s+ means whitespace characters 1 or more times.

3 Comments

It would be valid without the whitespace, so I would use \s*. The array would also be valid using "key", instead of 'key', so you'd need to account for that. Same goes for value. It's much better just to read this as a PHP array, as mentioned by @t1gor
ow.. add $ in the end of the pattern (without backslash)
@shift66 I think the issue is that wintercounter is not using a delimiter, see : php.net/manual/en/regexp.reference.delimiters.php. By telling him to add the $ to the end are you not causing $ to be the delimiter used? This changes your regex?
0

Here is your regex:

\$conf(\[('.*')\])+\s?=\s?(.*);

The first matches are your keys, last one your value

2 Comments

This is probably because the first "\" escapes the string in PHP.
This example does make some assumptions as to the formatting of the array. It must always have 1 or 0 spaces, so no TABs or multiple spaces should have been added. The key is always designated by a ' and not ever using ". It will also capture the quotes around a value, so that may well need trimming. All-in-all I would consider using the options posted by @t1gor or @WebnetMobile.com
0

As this is php array you can directly loop through it, and get required key value pairs else you can you can use php's functions array_keys for fetching all keys from the $conf array and array_values for getting array values

or else

if you want to create a config editor try using ini file php has a good support for creating and using config files see this function parse_ini_file this is a standard way for creating config file if you have too key value pairs

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.