0

I have this string (it's an apple .strings file):

/* this is a comment line; it may contain semicolons */
"you have here some text; also may contain semicolons" = "some other text;";

/* this is a comment line; it may contain semicolons */
"you have here some text; also may contain semicolons" = "some other text;";

and so on.

I need to split this string after the semicolons at the end of each non-commented line.

If I use explode(";\n", $string); may not be accurate as the line could end in a ;(whitespace)(new line)

Update: The output should be an array. Each element should contain the commented line (if exists) and the quoted strings line.

Marvin Saldinger
  • 1,290
  • 1
  • 16
  • 34
  • From your example .. what is your expected output – Baba Dec 03 '12 at 12:09
  • You can use `;\s*\n` to allow whitespaces between the colon and the line end. - or simple match all key/value pairs directly, using `preg_match_all` and `"([^"]+)"\s*=\s*"([^"]+)"\*;`. if the values may containt escaped versions of `"` use something like `^"(.*?)"\s*=\s*"(.*?)";\s*\n$` – dognose Dec 03 '12 at 12:10

2 Answers2

1

You might be best simply reading the file line by line, testing for comment characters using strpos(), and using str_getcsv() for parsing the file lines rather than regexp

Mark Baker
  • 209,507
  • 32
  • 346
  • 385
1

If you're only concerned about the optional whitespace, you might get along with

$array = preg_split("/;\s*\n/", $string); 

preg_split takes a regular expression while explode takes a string as delimiter.

ditscheri
  • 579
  • 2
  • 9