Search code examples
phpstringassociative-arrayinitext-parsing

Parse an ini-formatted string to generate an associative array


I have a string like below.

$str = "ENGINE=InnoDB 
        DEFAULT CHARSET=utf8 
        COLLATE=utf8_unicode_ci 
        COMMENT='Table comment'";

And I need to parse the key/value pairs from the string and combine them with the key/value pairs in the array below...

$arr = array(
    "ENGINE" => "InnoDB",
    "DEFAULT CHARSET" => "utf8",
    "COLLATE" => "utf8_unicode_ci",
    "COMMENT" => "'Table comment'"
);

Here the sequence of the parts of the string can be different.

Example:

$str = "ENGINE=InnoDB
        COMMENT='Table comment'
        COLLATE=utf8_unicode_ci
        DEFAULT CHARSET=utf8";

Solution

  • You should use preg_match_all() and have PHP build your output from there in the format you'd like. Here's a working example in PHP. And the regex statement.

    <?php
        $str = "ENGINE=InnoDB COMMENT='Table comment' COLLATE=utf8_unicode_ci DEFAULT CHARSET=utf8";
        preg_match_all("/([\w ]+)=(\w+|'(?:[^'\\\]|\\.)+')\s*/",$str,$matches,PREG_SET_ORDER);
        $out = [];
        foreach($matches as $match) {
            $out[$match[1]] = $match[2];
        }
        var_dump($out);
    ?>
    

    And the result:

    array(4) {
      ["ENGINE"]=>
      string(6) "InnoDB"
      ["COMMENT"]=>
      string(15) "'Table comment'"
      ["COLLATE"]=>
      string(15) "utf8_unicode_ci"
      ["DEFAULT CHARSET"]=>
      string(4) "utf8"
    }
    

    Explanation of regex

    ([\w ]+) // match one or more word characters (alpha+underscore+space)
    = // match equals sign
      (
          \w+ // match any word character
       | // or
          ' // match one exact quote character
          (?:[^'\\]|\\.)+ // match any character including escaped quotes
          ' // match one exact quote character
       )
    \s* // match any amount of whitespace until next match