如何从可本地化的.PHP中的字符串格式文件


How to extract strings from a localizable.strings-formatted file in PHP?

我正在构建一个PHP web应用程序,用户可以从他们的ios/mac应用程序上传他们的字符串文件(由键和值组成)。我需要能够从这些文件中提取字符串,但似乎我的正则表达式技能是超越生锈。

例如,一个文件看起来像这样:

STRING1 = "hello";
"good = bye" = "good = bye";
NAME = "Your name is '"%@'"";
"semicolon;confusion" = "I love semicolons; I hate semicolons"; "forget new line" = "forgot new line!";

应该收益率:

[0] = ["STRING1","'"hello'""]
[1] = ["'"good = bye'"","'"good = bye'""]
[2] = ["NAME","'"Your name is '"%@'"'""]
[3] = ["'"semicolon;confusion'"","'"I love semicolons; I hate semicolons'""]
[4] = ["'"forget new line'"","'"forgot new line!'""]

谢谢!

试试这个:

$data = array();
$lines = file('filename.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
foreach($lines as $line)
    while(preg_match('/^''s*("(?:[^"]|''''")*"|[^"]''w*)''s*=''s*("(?:[^"]|''''")*"|[^"]''w*)''s*;/', $line, $groups) == 1) {
        // $group[1] contains the name, $group[2] contains the value
        array_push($data, array($groups[1], $groups[2])); 
        $line = trim(substr($line, strlen($groups[0])));
    }

我假设输入存在一个名为filename.txt的文件中,并使用它来填充行$lines的数组。如果需要的话,调整代码以另一种方式填充$lines应该是相当容易的。

如果您在这段代码的底部使用var_dump($data),那么对于您上面给出的输入,您将得到以下输出:

array(5) {
    [0]=> array(2) {
        [0]=> string(7) "STRING1"
        [1]=> string(7) ""hello""
    }
    [1]=> array(2) {
        [0]=> string(12) ""good = bye""
        [1]=> string(12) ""good = bye""
    }
    [2]=> array(2) {
        [0]=> string(4) "NAME"
        [1]=> string(21) ""Your name is "%@"""
    }
    [3]=> array(2) {
        [0]=> string(21) ""semicolon;confusion""
        [1]=> string(38) ""I love semicolons; I hate semicolons""
    }
    [4]=> array(2) {
        [0]=> string(17) ""forget new line""
        [1]=> string(18) ""forgot new line!""
    }
}

我相信这就是你想要的数据格式。

如果它们看起来像您的示例中的对,则可以使用如下表达式进行匹配:

(?x)
(?<key> 'w++ | " (?: [^"'']++ | '' . )*+ " )
's*+ = 's*+
(?<val> " (?: [^"'']++ | '' . )*+ " )
's*+ ;

如果您希望允许不同的引号字符或非引号值,请相应地更改它。

的例子:

$str = <<<'__EOS__'
STRING1 = "hello";
"good = bye" = "good = bye";
NAME = "Your name is '"%@'"";
"semicolon;confusion" = "I love semicolons; I hate semicolons"; "forget new line" = "forgot new line!";
__EOS__;
$re = <<<'__EOS__'
/
(?<key> 'w++ | " (?: [^"'']++ | '' . )*+ " )
's*+ = 's*+
(?<val> " (?: [^"'']++ | '' . )*+ " )
's*+ ;
/x
__EOS__;
preg_match_all($re, $str, $matches);
var_dump($matches);

preg_match_all:

$str = <<< EOF
STRING1 = "hello";
"good = bye" = "good1 = bye1";
NAME = "Your name is '"%@'"";
"semicolon;confusion" = "I love semicolons; I hate semicolons"; "forget new line" = "forgot new line!";
EOF;
if (preg_match_all('~(?<key>.+?)'s+='s+(?=(?:(?:[^"]*"){2})*[^"]*$)(?<val>.+?)'s*(?<=");~', $str, $arr))
   print_r($arr);

然后使用数组$key$val来获取您的值。

实时演示:http://ideone.com/9SIikc