【问题标题】:preg match all in array searching for [ ]preg 匹配所有数组搜索 [ ]
【发布时间】:2013-08-08 12:25:29
【问题描述】:

我找到了解决方案 mysqlf 使用:

foreach ($output as $value) {
    if (strpos($value, "]:") > -1) {
        $tal = substr($value, strpos($value, "]:") +3) . "<br>";
        echo $tal;
    }
}

这会返回: -210 -212


提前致谢。

我想怀孕所以我只得到这条线:[10] => [147]:-210 或 [10] => [147]: -210 和 [21] => [148]: -212

我怎样才能怀孕[147]: 或者有更好的方法来获取具体信息?

我的数组 $output 包含:

Array
(
    [0] => modpoll 3.4 - FieldTalk(tm) Modbus(R) Master Simulator
    [1] => Copyright (c) 2002-2013 proconX Pty Ltd
    [2] => Visit http://www.modbusdriver.com for Modbus libraries and tools.
    [3] => 
    [4] => Protocol configuration: MODBUS/TCP
    [5] => Slave configuration...: address = 1, start reference = 147, count = 1
    [6] => Communication.........: 10.234.6.11, port 502, t/o 1.00 s, poll rate 1000 ms
    [7] => Data type.............: 16-bit register, output (holding) register table
    [8] => 
    [9] => -- Polling slave...
    [10] => [147]: -210
    [11] => modpoll 3.4 - FieldTalk(tm) Modbus(R) Master Simulator
    [12] => Copyright (c) 2002-2013 proconX Pty Ltd
    [13] => Visit http://www.modbusdriver.com for Modbus libraries and tools.
    [14] => 
    [15] => Protocol configuration: MODBUS/TCP
    [16] => Slave configuration...: address = 1, start reference = 148, count = 1
    [17] => Communication.........: 10.234.6.11, port 502, t/o 1.00 s, poll rate 1000 ms
    [18] => Data type.............: 16-bit register, output (holding) register table
    [19] => 
    [20] => -- Polling slave...
    [21] => [148]: -212
)

$matches  = preg_grep ('/^[147] (\w+)/i', $output);
print_r ($matches);
//only returns Array()

【问题讨论】:

    标签: php arrays regex preg-match-all modbus


    【解决方案1】:

    您需要转义左方括号,因为[147] 被视为包含147 的字符类

    你可以这样做:

    $result=preg_grep('~^\[14(?:7|8)]:~',$rgData);
    print_r($result);
    

    您可以找到关于转义(或不转义)方括号here的所有信息

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多