如何解析 PHP 中的字符串?

How to parse string in PHP?

我从服务器获取字符串:

auto  status
        simple.cars  OK
        simple.moto  OK
        authorize.cars  OK
        authorize.moto  OK

我想通过制表来解析它并得到一个数组键 (simple.cars) - 值 OK。

$math = preg_match_all("/^(.*)[\t\s]+(OK|FAIL)$/im", $result, $out);
        var_dump($out);

但它 returns 我是空数组。

这是构建包含键及其 OK/FAIL 值的映射的一种方法:

$input = "auto  status\n         simple.cars  OK\n         simple.moto  OK\n         authorize.cars  OK\n 
    authorize.moto  OK";
preg_match_all("/(\S+)\s+(OK|FAIL)/", $input, $matches);
$map = array();
for ($i=0; $i < sizeof($matches[1]); ++$i) {
    $map[$matches[1][$i]] = $matches[2][$i];
}
print_r($map);

这会打印:

Array
(
    [simple.cars] => OK
    [simple.moto] => OK
    [authorize.cars] => OK
    [authorize.moto] => OK
)