Melvin Melvin - 3 years ago 188
PHP Question

Reading information from CSS in PHP

I am creating a website where users shall be able to upload plugins with a file called 'info.css'. I want my PHP-file to be able to read out information from this file, for example the ID.

The 'info.css' file will contain something similar to:

ID: test-preset;
Name: Test Preset;

I want the ID and Name to get into separate strings, without the 'id:' or 'name:'.
Please write any solution you may will work. I have tried with following (but have gotten stuck on the way. Please note that the information in the 'info.css' file may appear in a different order, so for example it should work if the 'Name:' comes first.

$preset_inf = strtolower($preset_inf);
$preset_inf = explode('*/', $preset_inf);
$preset_inf = str_replace('/*', '', $preset_inf[0]);
$preset_inf = str_replace(' ', '', $preset_inf);
$preset_inf = explode(';', $preset_inf);

Answer Source

You can use regex to retrieve each variable, so:

preg_match( '/Name: (.*?);/', $css_file, $match );
$name = $match[1];
echo $name;

preg_match( '/ID: (.*?);/', $css_file, $match );
$id = $match[1];
echo $id;

Would return

Test Preset
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download