0

我有这个字符串(它是一个苹果 .strings 文件):

/* 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;";

等等。

我需要在每个非注释行末尾的分号之后拆分此字符串。

如果我使用explode(";\n", $string);可能不准确,因为该行可能以 ;(whitespace)(new line) 结尾

更新:输出应该是一个数组。每个元素都应包含注释行(如果存在)和引用的字符串行。

4

2 回答 2

1

如果您只关心可选的空格,您可能会相处融洽

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

preg_split采用正则表达式,而explode采用字符串作为分隔符。

于 2012-12-03T12:21:54.237 回答
1

您可能最好简单地逐行读取文件,使用strpos()测试注释字符,并使用str_getcsv()来解析文件行而不是正则表达式

于 2012-12-03T11:51:46.393 回答