首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >preg匹配数组中的所有搜索[]

preg匹配数组中的所有搜索[]
EN

Stack Overflow用户
提问于 2013-08-08 12:25:29
回答 1查看 112关注 0票数 0

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

代码语言:javascript
复制
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包含:

代码语言:javascript
复制
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()
EN

回答 1

Stack Overflow用户

发布于 2013-08-08 13:22:25

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

你可以这样做:

代码语言:javascript
复制
$result=preg_grep('~^\[14(?:7|8)]:~',$rgData);
print_r($result);

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18125994

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档