1

我正在构建一个 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!\""]

谢谢!

4

3 回答 3

1

试试这个:

$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!""
    }
}

我相信这是您期望的数据格式。

于 2013-05-27T16:24:24.100 回答
0

如果它们在您的示例中看起来像,则可以与如下表达式匹配:

(?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);
于 2013-05-27T16:11:36.057 回答
0

你可以 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

于 2013-05-27T16:47:27.613 回答