Take the 2-minute tour ×

I've created a .info file similar to how you would in drupal.

Template Name = Valley

styles[] = styles/styles.css, styles/media.css
scripts[] = js/script.js

I want to use PHP get each variable and their values. For example I'd like to put the Template Name value to a PHP variable called Template Name and put the styles[] values in an array if there is mroe than one.

I'd also need to avoid it picking up on comments that are defined be a hash # before the text.

It seems a lot to ask, bt I'm really not sure how to go about doing this. If someone has a solution I'd be very greatful, however if someone could just point me in the right direction that'll be just as helpful.

Thanks in advanced!

share|improve this question
what have you tried? –  Bazzz Nov 29 '11 at 10:52
That looks like an .ini file format. Would that be what Drupal uses to read it in by any chance? –  mario Nov 29 '11 at 10:56
I've created a function, here is what I've input to the function - split_sheets(strstr($doc, "styles[]")). The $doc is an fread. The function then splits the string with explode twice, fist searching for a = the spliting that with an explode searching for , . This worked for a single line, but when I added anymore they conflicted with each other. –  PapaSmurf Nov 29 '11 at 11:03
I'm not sure how drupal reads it. –  PapaSmurf Nov 29 '11 at 11:04
add comment

3 Answers

up vote 1 down vote accepted

If you can adkust your info file slightly, you can use a built-in PHP function:


TemplateName = Valley

styles[] = "styles/styles.css"
styles[] = "styles/media.css"
scripts[] = "js/script.js"

which will result in an array

share|improve this answer
That's probaly the best way of doing it! I'll give it ago and report back, thanks! –  PapaSmurf Nov 29 '11 at 11:06
That has worked a treat, thanks! –  PapaSmurf Nov 29 '11 at 11:12
add comment

If all you're after is something "similar" you could take a look at the parse_ini_file() function.

share|improve this answer
add comment

Drupal was a good hint:

function drupal_parse_info_file($filename) {
  $info = array();
  $constants = get_defined_constants();

  if (!file_exists($filename)) {
    return $info;

  $data = file_get_contents($filename);
  if (preg_match_all('
    @^\s*                           # Start at the beginning of a line, ignoring leading whitespace
      [^=;\[\]]|                    # Key names cannot contain equal signs, semi-colons or square brackets,
      \[[^\[\]]*\]                  # unless they are balanced and not nested
    \s*=\s*                         # Key/value pairs are separated by equal signs (ignoring white-space)
      ("(?:[^"]|(?<=\\\\)")*")|     # Double-quoted string, which may contain slash-escaped quotes/slashes
      (\'(?:[^\']|(?<=\\\\)\')*\')| # Single-quoted string, which may contain slash-escaped quotes/slashes
      ([^\r\n]*?)                   # Non-quoted string
    )\s*$                           # Stop at the next end of a line, ignoring trailing whitespace
    @msx', $data, $matches, PREG_SET_ORDER)) {
    foreach ($matches as $match) {
      // Fetch the key and value string
      $i = 0;
      foreach (array('key', 'value1', 'value2', 'value3') as $var) {
        $$var = isset($match[++$i]) ? $match[$i] : '';
      $value = stripslashes(substr($value1, 1, -1)) . stripslashes(substr($value2, 1, -1)) . $value3;

      // Parse array syntax
      $keys = preg_split('/\]?\[/', rtrim($key, ']'));
      $last = array_pop($keys);
      $parent = &$info;

      // Create nested arrays
      foreach ($keys as $key) {
        if ($key == '') {
          $key = count($parent);
        if (!isset($parent[$key]) || !is_array($parent[$key])) {
          $parent[$key] = array();
        $parent = &$parent[$key];

      // Handle PHP constants.
      if (isset($constants[$value])) {
        $value = $constants[$value];

      // Insert actual value
      if ($last == '') {
        $last = count($parent);
      $parent[$last] = $value;

  return $info;

Source, this function is part of the drupal code-base, drupal's license applies, used for documentation purposes here only.

share|improve this answer
Thanks for that, I think i'll just use the parse ini function from PHP. This could be useful someone else though, thanks! –  PapaSmurf Nov 29 '11 at 11:11
add comment

Your Answer


