我从服务器获取字符串:
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);
但它返回我空数组。
这是构建包含键及其 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
)