Melvin
Melvin

Reputation: 117

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); 

Upvotes: 0

Views: 87

Answers (5)

Oleg Lubimoff
Oleg Lubimoff

Reputation: 144

Scalable solution.

$presetInfoItem = [];
$presetInfo = [];
$presetFile = "/*
ID: test-preset;
Name: Test Preset;
*/";

$fields = ['ID', 'Name'];
foreach ($fields as $field) {
    $matchesCount = preg_match_all("@$field:(?'$field'[\w-\s]*);@", $presetFile, $presetInfoItem);
    if ($matchesCount === 0 || $matchesCount === false) {
        $presetInfo[$field] = "";
    } else {
        $presetInfo[$field] = trim($presetInfoItem[$field][0]);
    }
}
var_export($presetInfo);

Upvotes: 1

TheGentleman
TheGentleman

Reputation: 2352

In case you need a more general solution, here is a regex that will parse a header with an arbitrary number of options along with their names:

$string = '/*
ID: test-preset;
Name: Test Preset;
*/';

$pattern = '/^(?!\/\*)([^:]+):([^:]+);$/mU';

preg_match_all($pattern, $string, $matches, PREG_SET_ORDER, 0);
$results = array();
foreach($matches as $match){
    $results[$match[1]] = $match[2];
}

$results now contains an array with this structure:

[
    "ID" => "test-preset",
    "Name" => "Test Preset"
]

This has the benefit of being able to handle any number of "Header arguments".

Upvotes: 1

Kamran Khan
Kamran Khan

Reputation: 453

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
test-preset

Upvotes: 1

Just Vlad
Just Vlad

Reputation: 99

For your pleasure:

<?php

$css = '/*
ID: test-preset;
Name: Test Preset;
*/';

$css = str_replace("*/", "", $css);
$css = str_replace("/*", "", $css);
$css = str_replace(";", "", $css);
$css = trim($css);

$lines = explode("\n", str_replace("\r", '', $css)); 

if(!empty($lines)) {

    foreach($lines as $i => $line) {

        $vals = explode(":", $line);

        $key = $vals[0];
        $value = $vals[1];

        echo '<div><b>'.$key.'</b>: '.$value.'</div>';

    }

}


?>

Result is:

ID: test-preset
Name: Test Preset

Regex is not needed :)

Upvotes: 0

Andreas
Andreas

Reputation: 23958

Regex?

$str = "/*
ID: test-preset;
Name: Test Preset;
*/";

 preg_match_all("/(ID|Name):\s*(.*?)\;/s", $str, $m);


var_dump($m);

This will produce:

array(3) {
  [0]=>
      string(35) "ID: test-preset;
      Name: Test Preset;"
  [1]=>
      string(11) "test-preset"
  [2]=>
      string(11) "Test Preset"
}

Matches anything between ID/Name and ;.

Edit noticed it could be the other way around too. Edited the code.
The output array will look slightly different but the part you want is in $m[2] array.

https://3v4l.org/iusmV

Upvotes: 4

Related Questions