0

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

5 Answers 5

4

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

Sign up to request clarification or add additional context in comments.

Comments

1

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

1 Comment

This solution was the best: the css info file could be in another order without any problems. Thanks!
1

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".

Comments

1

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

Comments

0

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

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.