Working to split an array at multiple delimiters, and create key=>value

So I’m working on a project that will take a saved configuration from a MariaDB server and parse it though PHP, creating a readable array with a key value relationship, here’s the current data from the database:

item1=value1 item2=value2 item3=value3 item4=value4 item5=value5 item6=value6 item7=value7 item8=value8 item9=value9

Now, I can get these values from the database fine. But what I want to do is take each of these values is set the value before the equal sign (=) as they key, and the value after is as the value. I also need it to ignore or not display certain values, like

item10=value10 item11=value11 item12=value12

Here is the code I am currently playing with:
[php]
foreach($stmt->fetchAll() as $k=>$v) {
// this sanitizes the configuration file so all un
// $v['savedconfig] is data coming from db

$allsettings = $v['savedconfig'];

$settings = explode(' ', $allsettings);
//$settings = explode('=', $allsettings);

print_r($settings);

}

[/php]

Needless to say, this code is returning everything and while it does split at the = sign, it keeps newline and spaces in the array instead of splitting them into a new key. I’ve used numerous resources including StackOverlow and links I’ve found here (including a code another developer wrote for me on a separate project that was modified to work with this) and they all return errors, so I’m out of ideas.

Any help to get me pointed in the right direction would be greatly appreciated.

Forget the code for a minute. Post an SQL dump of your DB so we can see what you are actually working with. This appears to be a classic XY Problem.

I just sent you the exact code i’m dealing with.

there is one column that contains

item1=value1 item2=value2 item3=value3 item4=value4 item5=value5 item6=value6 item7=value7 item8=value8 item9=value9 item10=value10 item11=value11 item12=value12

I didn’t ask for code. So your database really has data in a single rows called itemX=valueX?

No, there is an id column (automatically incremented) and then a column for if it’s been applied to the configuration. However, this is a test script. I have no relevant data to post other than the example data I’ve already provided.

Ok, if I understand what you have…

Why are you planning on storing the data that way? You should have a column for the Key data and a column for the Value Data.

Edit* Wait a minute, do you mean the actual MariaDB settings stored in the DB is what you want to parse?

No, this is a temporary holding space for a configuration file that is formatted the way I’ve shown.

I want to pull that information from the database, the reason its formatted that way is because when the PHP script writes the file, it has to be in that format for the binary to run it.

Let’s see if I got this…

So you have a config file that you imported to the DB that you now want to pull back out?

If so… Let’s start with the actual file you imported, then we can deal with the output. Post or attach the original data file you started from.

What are these config settings for? An application? Something else? Where does this config file come from in the first place?

What I originally posted, is the actual file that was imported. It looks the same in the database as it does in the final .conf file that is generated for the binary.

These settings are for binary that requires it’s configuration file to be written like what I’ve posted previously. Which is why the database and the conf file both match it’s format. Once the user clicks “apply” it will overwrite the original .conf file and then restart the binary, using the new settings file that was generated from the data in the database.

  • EDIT: Incorrect response deleted.

While I appreciate the help, I need it for display, not for writing the configuration file, that part has already been done.

Essentially, to edit the file, users will be presented with form fields for the fields they’re allowed to change, as I said above item/value 1-9 should be displayed and editable, while item/value 10-12 should be, or should be displayed as read only or text.

I probably should have included that (my apologies), that is what I am attempting to do. In hindsight I can see where I forgot to mention that. I was wrapped up in the project I forgot to mention that key detail.

Now that I think about it, you are storing the data wrong. Each config setting should be a column, not all in rows as you are doing. And herein lies the problem with XY posts.

That would require me to add additional fields. Multiple configuration files will be stored in the database. Currently there is only one. So I will need to add identifiers to ensure the correct settings are being displayed to the correct user. Correct me if I’m wrong, but that seems like a security issue. Not all configuration files will use the same settings, some will use base settings that are already predefined and are not editable by the end users.

Perhaps you should give a better explanation of exactly what this project is. And what is this “binary” you mentioned. I think we are still in XY mode.

I am working on building a streaming media control panel. The binary is a version of Shoutcast v2, which uses both user and predefined .conf files to allow the server to run.

The user will edit the conf file from the browser, and click save. Once they click save it will update it in the database, but not write the configuration file or restart the binary executable.

When they click apply, it will restart the binary file, and grab the newest settings from the new conf that was written based on the database updates. I’m trying to restrict them from changing the port, maxlisteners, max bitrate and where logs are stored. Everything else should be editable, with the exception of the main included files (which I have hidden from the code already).

This should have been your first post. At least we are getting somewhere now, I will need some time to look into this.

I was able to solve this. I ended up adding a column for each setting, then generating the end configuration file using PHP and the file_put_contents function.

Sponsor our Newsletter | Privacy Policy | Terms of Service