tags:

views:

80

answers:

1

I need a PHP Regex that can parse .strings files. In particular, it needs to be able to handle comments, blank lines, escaped characters and angle brackets.

Example of a .strings file:

/* string one */
"StringOne"="\"1\"";

"StringTwo"="<2>";

/* Bob Dillon */
"Bob"="Dillon";

By request, the desired output would be something such as:

Array( [StringOne] => "\"1\"" [StringTwo] => "<2>" [Bob] => "Dillon" )

All help is greatly appreciated!

+1  A: 

this?

 $r = '
 /* string one */
 "StringOne"="first \"string\"";

 "StringTwo"="2";

 /* Bob Dillon */
 "Bob"="Dillon";
 ';

    preg_match_all('~^\s*"((?:\\\\.|[^"])*)"[^"]+"((?:\\\\.|[^"])*)"~m', 
         $r, $matches, PREG_SET_ORDER);
 $parsed = array();
 foreach($matches as $m)
    $parsed[$m[1]] = $m[2];

 print_r($parsed);

prints

Array ( 
[StringOne] => 1 
[StringTwo] => 2 
[Bob] => Dillon 
)
stereofrog
That doesn't handle brackets or escaped characters (see updated question). Closer than what I got to though!
Sorry, I misspoke. It *does* handle angle brackets but it does NOT handle escaped parenthesis.
i don't understand what you mean by "handle". Can you provide examples of the desired output.
stereofrog
Updated question to provide example output.
code updated(at least 15 characters required? i have really nothing more to say...)
stereofrog