匹配特定格式的多行文本
Match multiple lines of text with specific format
我正在向设备发送一些 telnet 命令并在数组中读回其结果值。
我需要过滤数据,但我不确定 preg_split()
或 preg_match()
哪个更适合。
隔离所需的文本行后,我需要将文本的几个部分存储在 mysql table 中——对于每一行。
所有需要存储的结果格式如下:0/0 x x xx:xx:xx:xx:xx:xx x x x x x
.
下面是我正在使用的代码,但它只是过滤第一个数组列。
$show_pon[0] = "show ont info 1 all";
$show_pon[1] = "show ont info 2 all";
$show_pon[2] = "show ont info 3 all";
$show_pon[3] = "show ont info 4 all";
$show_pon[4] = "show ont info 5 all";
$show_pon[5] = "show ont info 6 all";
$show_pon[6] = "show ont info 7 all";
$show_pon[7] = "show ont info 8 all";
$show_pon[8] = "show ont info 9 all";
$show_pon[9] = "show ont info 10 all";
$show_pon[10] = "show ont info 11 all";
$show_pon[11] = "show ont info 12 all";
$show_pon[12] = "show ont info 13 all";
$show_pon[13] = "show ont info 14 all";
$show_pon[14] = "show ont info 15 all";
$show_pon[15] = "show ont info 16 all";
$telnet->DoCommand($show_pon, $data_response);
$sections = preg_split('/-{50,}\s*\r?\n/s', $data_response);
它实际上发送了 16 个不同的 cmd.. 并且输出回复到数组 $sections
下面的输出是..
Array
(
[0] => show ont info 1 all
[1] => F/S P ONT MAC Control Run Config Match Desc ID flag state state state
[2] => 0/0 1 1 AC:4E:91:A6:CF:F5 active online success match VILERMINA-.
[3] => Total: 1, online 1 OLT(config-interface-epon-0/0)# show ont info 2 all
[4] => F/S P ONT MAC Control Run Config Match Desc ID flag state state state
[5] => 0/0 2 1 E0:E8:E6:4E:62:58 active online success match CLOVES-NAV.
[6] => Total: 1, online 1 OLT(config-interface-epon-0/0)# show ont info 3 all
[7] => F/S P ONT MAC Control Run Config Match Desc ID flag state state state
[8] => 0/0 3 1 E0:67:B3:93:F1:97 active online success match 0/0 3 2 18:C5:8A:B0:19:4F active online success match
[9] => Total: 2, online 2 OLT(config-interface-epon-0/0)# show ont info 4 all
[10] => F/S P ONT MAC Control Run Config Match Desc ID flag state state state
[11] => 0/0 4 1 E0:67:B3:A6:45:E1 active online success match Jessica-Si. 0/0 4 2 E0:67:B3:7E:20:05 active online success match onu-id-2 0/0 4 3 E0:67:B3:7D:F8:F9 active online success match onu-id-3 0/0 4 4 E0:E8:E6:4E:69:B8 active online success match onu-id-4
[12] => Total: 4, online 4 OLT(config-interface-epon-0/0)# show ont info 5 all
[13] => F/S P ONT MAC Control Run Config Match Desc ID flag state state state
[14] => 0/0 5 1 C4:70:0B:26:B9:38 active online success match 0/0 5 2 E0:67:B3:88:33:E2 active online success match 0/0 5 3 E0:E8:E6:4E:70:B8 active powerdown initial initial 0/0 5 4 C4:70:0B:26:5C:28 active online success match 0/0 5 5 E0:E8:E6:4E:75:C8 active online success match 0/0 5 6 E0:E8:E6:4E:69:C0 active online success match 0/0 5 7 E0:67:B3:93:EF:D5 active online success match
[15] => Total: 7, online 6 OLT(config-interface-epon-0/0)# show ont info 6 all
[16] => F/S P ONT MAC Control Run Config Match Desc ID flag state state state
[17] => 0/0 6 1 E0:67:B3:88:3D:57 active online success match 0/0 6 2 E0:E8:E6:4E:71:08 active online success match 0/0 6 3 E0:E8:E6:4E:74:78 active online success match
[18] => Total: 3, online 3 OLT(config-interface-epon-0/0)# show ont info 7 all
[19] => F/S P ONT MAC Control Run Config Match Desc ID flag state state state
[20] => 0/0 7 1 00:6D:61:59:D8:48 active online success match 0/0 7 2 E0:E8:E6:4E:55:20 active online success match
[21] => Total: 2, online 2 OLT(config-interface-epon-0/0)# show ont info 8 all
[22] => F/S P ONT MAC Control Run Config Match Desc ID flag state state state
[23] => 0/0 8 1 E0:E8:E6:4E:69:E0 active online success match 0/0 8 2 E0:E8:E6:4E:70:08 active online success match
[24] => Total: 2, online 2 OLT(config-interface-epon-0/0)# show ont info 9 all
[25] => F/S P ONT MAC Control Run Config Match Desc ID flag state state state
[26] => 0/0 9 1 E0:E8:E6:4E:62:78 active online success match 0/0 9 2 E0:67:B3:A6:58:0F active online success match 0/0 9 3 9C:28:EF:01:AE:99 active online success match
[27] => Total: 3, online 3 OLT(config-interface-epon-0/0)# show ont info 10 all
[28] => F/S P ONT MAC Control Run Config Match Desc ID flag state state state
[29] => 0/0 10 1 E0:67:B3:35:CE:9B active online success match 0/0 10 2 E0:67:B3:A6:56:71 active online success match 0/0 10 3 E0:67:B3:A6:25:33 active online success match
[30] => Total: 3, online 3 OLT(config-interface-epon-0/0)# show ont info 11 all
[31] => F/S P ONT MAC Control Run Config Match Desc ID flag state state state
[32] => 0/0 11 1 E0:67:B3:7D:F3:E0 active online success match
[33] => Total: 1, online 1 OLT(config-interface-epon-0/0)# show ont info 12 all
[34] => F/S P ONT MAC Control Run Config Match Desc ID flag state state state
[35] => 0/0 12 1 E0:67:B3:93:F9:11 active online success match
[36] => Total: 1, online 1 OLT(config-interface-epon-0/0)# show ont info 13 all Error: There is no ONT available. OLT(config-interface-epon-0/0)# show ont info 14 all Error: There is no ONT available. OLT(config-interface-epon-0/0)# show ont info 15 all Error: There is no ONT available. OLT(config-interface-epon-0/0)# show ont info 16 all Error: There is no ONT available. OLT(config-interface-epon-0/0)# OLT(config-interface-epon-0/0)#
)
现在我需要过滤这个数组,只提取或存储在另一个数组中只是以
开头的数组
0/0 x x xxxxxxxxxx x x x x x
基本上是命令回复,我已经请求了 16 个命令,即设备上的 16 个不同端口,每个端口使用该设备的数据回复连接到该端口的设备
0/0 1 1 00:00:00:00:00 xxxxx xxxxx xxxx xxxx xxxx
下面更新完整的 telnet cmd 回复日志以便更好地理解
OLT(config-interface-epon-0/0)# show ont info 1 all
-----------------------------------------------------------------------------
F/S P ONT MAC Control Run Config Match Desc
ID flag state state state
----------------------------------------------------------------------------
0/0 1 1 AC:4E:91:A6:CF:F5 active online success match VILERMINA-.
-----------------------------------------------------------------------------
Total: 1, online 1
OLT(config-interface-epon-0/0)# show ont info 2 all
-----------------------------------------------------------------------------
F/S P ONT MAC Control Run Config Match Desc
ID flag state state state
----------------------------------------------------------------------------
0/0 2 1 E0:E8:E6:4E:62:58 active online success match CLOVES-NAV.
-----------------------------------------------------------------------------
Total: 1, online 1
OLT(config-interface-epon-0/0)# show ont info 3 all
-----------------------------------------------------------------------------
F/S P ONT MAC Control Run Config Match Desc
ID flag state state state
----------------------------------------------------------------------------
0/0 3 1 E0:67:B3:93:F1:97 active online success match
0/0 3 2 18:C5:8A:B0:19:4F active online success match
-----------------------------------------------------------------------------
Total: 2, online 2
OLT(config-interface-epon-0/0)# show ont info 4 all
-----------------------------------------------------------------------------
F/S P ONT MAC Control Run Config Match Desc
ID flag state state state
----------------------------------------------------------------------------
0/0 4 1 E0:67:B3:A6:45:E1 active online success match Jessica-Si.
0/0 4 2 E0:67:B3:7E:20:05 active online success match onu-id-2
0/0 4 3 E0:67:B3:7D:F8:F9 active online success match onu-id-3
0/0 4 4 E0:E8:E6:4E:69:B8 active online success match onu-id-4
-----------------------------------------------------------------------------
Total: 4, online 4
OLT(config-interface-epon-0/0)# show ont info 5 all
-----------------------------------------------------------------------------
F/S P ONT MAC Control Run Config Match Desc
ID flag state state state
----------------------------------------------------------------------------
0/0 5 1 C4:70:0B:26:B9:38 active online success match
0/0 5 2 E0:67:B3:88:33:E2 active online success match
0/0 5 3 E0:E8:E6:4E:70:B8 active powerdown initial initial
0/0 5 4 C4:70:0B:26:5C:28 active online success match
0/0 5 5 E0:E8:E6:4E:75:C8 active online success match
0/0 5 6 E0:E8:E6:4E:69:C0 active online success match
0/0 5 7 E0:67:B3:93:EF:D5 active online success match
-----------------------------------------------------------------------------
Total: 7, online 6
OLT(config-interface-epon-0/0)# show ont info 6 all
-----------------------------------------------------------------------------
F/S P ONT MAC Control Run Config Match Desc
ID flag state state state
----------------------------------------------------------------------------
0/0 6 1 E0:67:B3:88:3D:57 active online success match
0/0 6 2 E0:E8:E6:4E:71:08 active online success match
0/0 6 3 E0:E8:E6:4E:74:78 active online success match
-----------------------------------------------------------------------------
Total: 3, online 3
OLT(config-interface-epon-0/0)# show ont info 7 all
-----------------------------------------------------------------------------
F/S P ONT MAC Control Run Config Match Desc
ID flag state state state
----------------------------------------------------------------------------
0/0 7 1 00:6D:61:59:D8:48 active online success match
0/0 7 2 E0:E8:E6:4E:55:20 active online success match
-----------------------------------------------------------------------------
Total: 2, online 2
OLT(config-interface-epon-0/0)# show ont info 8 all
-----------------------------------------------------------------------------
F/S P ONT MAC Control Run Config Match Desc
ID flag state state state
----------------------------------------------------------------------------
0/0 8 1 E0:E8:E6:4E:69:E0 active online success match
0/0 8 2 E0:E8:E6:4E:70:08 active online success match
-----------------------------------------------------------------------------
Total: 2, online 2
OLT(config-interface-epon-0/0)# show ont info 9 all
-----------------------------------------------------------------------------
F/S P ONT MAC Control Run Config Match Desc
ID flag state state state
----------------------------------------------------------------------------
0/0 9 1 E0:E8:E6:4E:62:78 active online success match
0/0 9 2 E0:67:B3:A6:58:0F active online success match
0/0 9 3 9C:28:EF:01:AE:99 active online success match
-----------------------------------------------------------------------------
Total: 3, online 3
OLT(config-interface-epon-0/0)# show ont info 10 all
-----------------------------------------------------------------------------
F/S P ONT MAC Control Run Config Match Desc
ID flag state state state
----------------------------------------------------------------------------
0/0 10 1 E0:67:B3:35:CE:9B active online success match
0/0 10 2 E0:67:B3:A6:56:71 active online success match
0/0 10 3 E0:67:B3:A6:25:33 active online success match
-----------------------------------------------------------------------------
Total: 3, online 3
OLT(config-interface-epon-0/0)# show ont info 11 all
-----------------------------------------------------------------------------
F/S P ONT MAC Control Run Config Match Desc
ID flag state state state
----------------------------------------------------------------------------
0/0 11 1 E0:67:B3:7D:F3:E0 active online success match
-----------------------------------------------------------------------------
Total: 1, online 1
OLT(config-interface-epon-0/0)# show ont info 12 all
-----------------------------------------------------------------------------
F/S P ONT MAC Control Run Config Match Desc
ID flag state state state
----------------------------------------------------------------------------
0/0 12 1 E0:67:B3:93:F9:11 active online success match
-----------------------------------------------------------------------------
Total: 1, online 1
OLT(config-interface-epon-0/0)# show ont info 13 all
Error: There is no ONT available.
OLT(config-interface-epon-0/0)# show ont info 14 all
Error: There is no ONT available.
OLT(config-interface-epon-0/0)# show ont info 15 all
Error: There is no ONT available.
OLT(config-interface-epon-0/0)# show ont info 16 all
Error: There is no ONT available.
不要费心使用 preg_split()
将您的文本分成几部分。看起来您正在保存数据,而不管它在哪个部分。
将 preg_match_all()
与可选的最后一个捕获组一起使用。
^
表示“匹配行的开头”,因为 m
模式修饰符。
\h*
表示“匹配零个或多个不代表换行符/换行符的白色space字符”。
\h+\
同上,只是需要出现一次或多次。
最后的水平 space 和可见字符是可选的——这就是带有 ?
的 non-capturing 组所做的。
代码:(Demo)
var_export(
preg_match_all(
'~^\h*(\d+/\d+)\h+(\d+)\h+(\d+)\h+([A-F\d]{2}(?::[A-F\d]{2}){5})\h+(\S+)\h+(\S+)\h+(\S+)\h+(\S+)(?:\h+(\S+))?~m',
$response,
$m,
PREG_SET_ORDER
)
? $m
: 'no matches'
);
查看示例文本和输出的演示。
作为更新,以下数组代码过滤了我需要的数据
$res = array_filter($sections, function ($var) { return (strpos($var, ' 0/0 ') == true);});
输出过滤后的最终结果
Array
(
[2] => 0/0 1 1 AC:4E:91:A6:CF:F5 active online success match VILERMINA-.
[5] => 0/0 2 1 E0:E8:E6:4E:62:58 active online success match CLOVES-NAV.
[8] => 0/0 3 1 E0:67:B3:93:F1:97 active online success match 0/0 3 2 18:C5:8A:B0:19:4F active online success match
[11] => 0/0 4 1 E0:67:B3:A6:45:E1 active online success match Jessica-Si. 0/0 4 2 E0:67:B3:7E:20:05 active online success match onu-id-2 0/0 4 3 E0:67:B3:7D:F8:F9 active online success match onu-id-3 0/0 4 4 E0:E8:E6:4E:69:B8 active online success match onu-id-4
[14] => 0/0 5 1 C4:70:0B:26:B9:38 active online success match 0/0 5 2 E0:67:B3:88:33:E2 active online success match 0/0 5 3 E0:E8:E6:4E:70:B8 active powerdown initial initial 0/0 5 4 C4:70:0B:26:5C:28 active online success match 0/0 5 5 E0:E8:E6:4E:75:C8 active online success match 0/0 5 6 E0:E8:E6:4E:69:C0 active online success match 0/0 5 7 E0:67:B3:93:EF:D5 active online success match
[17] => 0/0 6 1 E0:67:B3:88:3D:57 active online success match 0/0 6 2 E0:E8:E6:4E:71:08 active online success match 0/0 6 3 E0:E8:E6:4E:74:78 active online success match
[20] => 0/0 7 1 00:6D:61:59:D8:48 active online success match 0/0 7 2 E0:E8:E6:4E:55:20 active online success match
[23] => 0/0 8 1 E0:E8:E6:4E:69:E0 active online success match 0/0 8 2 E0:E8:E6:4E:70:08 active online success match
[26] => 0/0 9 1 E0:E8:E6:4E:62:78 active online success match 0/0 9 2 E0:67:B3:A6:58:0F active online success match 0/0 9 3 9C:28:EF:01:AE:99 active online success match
[29] => 0/0 10 1 E0:67:B3:35:CE:9B active online success match 0/0 10 2 E0:67:B3:A6:56:71 active online success match 0/0 10 3 E0:67:B3:A6:25:33 active online success match
[32] => 0/0 11 1 E0:67:B3:7D:F3:E0 active online success match
[35] => 0/0 12 1 E0:67:B3:93:F9:11 active online success match )
现在我只需要拆分其余数据以准备数组转发给查询 mysql
数组需要拆分成新的数组
[8] => 0/0 3 1 E0:67:B3:93:F1:97 active online success match 0/0 3 2 18:C5:8A:B0:19:4F active online success match
[11] => 0/0 4 1 E0:67:B3:A6:45:E1 active online success match Jessica-Si. 0/0 4 2 E0:67:B3:7E:20:05 active online success match onu-id-2 0/0 4 3 E0:67:B3:7D:F8:F9 active online success match onu-id-3 0/0 4 4 E0:E8:E6:4E:69:B8 active online success match onu-id-4
输出
[1] => 0/0 3 1 E0:67:B3:93:F1:97 active online success match
[2] => 0/0 3 2 18:C5:8A:B0:19:4F active online success match
[3] => 0/0 4 1 E0:67:B3:A6:45:E1 active online success match Jessica-Si.
[4] => 0/0 4 2 E0:67:B3:7E:20:05 active online success match onu-id-2
[5] => 0/0 4 3 E0:67:B3:7D:F8:F9 active online success match onu-id-3
[6] =>0/0 4 4 E0:E8:E6:4E:69:B8 active online success match onu-id-4
我正在向设备发送一些 telnet 命令并在数组中读回其结果值。
我需要过滤数据,但我不确定 preg_split()
或 preg_match()
哪个更适合。
隔离所需的文本行后,我需要将文本的几个部分存储在 mysql table 中——对于每一行。
所有需要存储的结果格式如下:0/0 x x xx:xx:xx:xx:xx:xx x x x x x
.
下面是我正在使用的代码,但它只是过滤第一个数组列。
$show_pon[0] = "show ont info 1 all";
$show_pon[1] = "show ont info 2 all";
$show_pon[2] = "show ont info 3 all";
$show_pon[3] = "show ont info 4 all";
$show_pon[4] = "show ont info 5 all";
$show_pon[5] = "show ont info 6 all";
$show_pon[6] = "show ont info 7 all";
$show_pon[7] = "show ont info 8 all";
$show_pon[8] = "show ont info 9 all";
$show_pon[9] = "show ont info 10 all";
$show_pon[10] = "show ont info 11 all";
$show_pon[11] = "show ont info 12 all";
$show_pon[12] = "show ont info 13 all";
$show_pon[13] = "show ont info 14 all";
$show_pon[14] = "show ont info 15 all";
$show_pon[15] = "show ont info 16 all";
$telnet->DoCommand($show_pon, $data_response);
$sections = preg_split('/-{50,}\s*\r?\n/s', $data_response);
它实际上发送了 16 个不同的 cmd.. 并且输出回复到数组 $sections
下面的输出是..
Array
(
[0] => show ont info 1 all
[1] => F/S P ONT MAC Control Run Config Match Desc ID flag state state state
[2] => 0/0 1 1 AC:4E:91:A6:CF:F5 active online success match VILERMINA-.
[3] => Total: 1, online 1 OLT(config-interface-epon-0/0)# show ont info 2 all
[4] => F/S P ONT MAC Control Run Config Match Desc ID flag state state state
[5] => 0/0 2 1 E0:E8:E6:4E:62:58 active online success match CLOVES-NAV.
[6] => Total: 1, online 1 OLT(config-interface-epon-0/0)# show ont info 3 all
[7] => F/S P ONT MAC Control Run Config Match Desc ID flag state state state
[8] => 0/0 3 1 E0:67:B3:93:F1:97 active online success match 0/0 3 2 18:C5:8A:B0:19:4F active online success match
[9] => Total: 2, online 2 OLT(config-interface-epon-0/0)# show ont info 4 all
[10] => F/S P ONT MAC Control Run Config Match Desc ID flag state state state
[11] => 0/0 4 1 E0:67:B3:A6:45:E1 active online success match Jessica-Si. 0/0 4 2 E0:67:B3:7E:20:05 active online success match onu-id-2 0/0 4 3 E0:67:B3:7D:F8:F9 active online success match onu-id-3 0/0 4 4 E0:E8:E6:4E:69:B8 active online success match onu-id-4
[12] => Total: 4, online 4 OLT(config-interface-epon-0/0)# show ont info 5 all
[13] => F/S P ONT MAC Control Run Config Match Desc ID flag state state state
[14] => 0/0 5 1 C4:70:0B:26:B9:38 active online success match 0/0 5 2 E0:67:B3:88:33:E2 active online success match 0/0 5 3 E0:E8:E6:4E:70:B8 active powerdown initial initial 0/0 5 4 C4:70:0B:26:5C:28 active online success match 0/0 5 5 E0:E8:E6:4E:75:C8 active online success match 0/0 5 6 E0:E8:E6:4E:69:C0 active online success match 0/0 5 7 E0:67:B3:93:EF:D5 active online success match
[15] => Total: 7, online 6 OLT(config-interface-epon-0/0)# show ont info 6 all
[16] => F/S P ONT MAC Control Run Config Match Desc ID flag state state state
[17] => 0/0 6 1 E0:67:B3:88:3D:57 active online success match 0/0 6 2 E0:E8:E6:4E:71:08 active online success match 0/0 6 3 E0:E8:E6:4E:74:78 active online success match
[18] => Total: 3, online 3 OLT(config-interface-epon-0/0)# show ont info 7 all
[19] => F/S P ONT MAC Control Run Config Match Desc ID flag state state state
[20] => 0/0 7 1 00:6D:61:59:D8:48 active online success match 0/0 7 2 E0:E8:E6:4E:55:20 active online success match
[21] => Total: 2, online 2 OLT(config-interface-epon-0/0)# show ont info 8 all
[22] => F/S P ONT MAC Control Run Config Match Desc ID flag state state state
[23] => 0/0 8 1 E0:E8:E6:4E:69:E0 active online success match 0/0 8 2 E0:E8:E6:4E:70:08 active online success match
[24] => Total: 2, online 2 OLT(config-interface-epon-0/0)# show ont info 9 all
[25] => F/S P ONT MAC Control Run Config Match Desc ID flag state state state
[26] => 0/0 9 1 E0:E8:E6:4E:62:78 active online success match 0/0 9 2 E0:67:B3:A6:58:0F active online success match 0/0 9 3 9C:28:EF:01:AE:99 active online success match
[27] => Total: 3, online 3 OLT(config-interface-epon-0/0)# show ont info 10 all
[28] => F/S P ONT MAC Control Run Config Match Desc ID flag state state state
[29] => 0/0 10 1 E0:67:B3:35:CE:9B active online success match 0/0 10 2 E0:67:B3:A6:56:71 active online success match 0/0 10 3 E0:67:B3:A6:25:33 active online success match
[30] => Total: 3, online 3 OLT(config-interface-epon-0/0)# show ont info 11 all
[31] => F/S P ONT MAC Control Run Config Match Desc ID flag state state state
[32] => 0/0 11 1 E0:67:B3:7D:F3:E0 active online success match
[33] => Total: 1, online 1 OLT(config-interface-epon-0/0)# show ont info 12 all
[34] => F/S P ONT MAC Control Run Config Match Desc ID flag state state state
[35] => 0/0 12 1 E0:67:B3:93:F9:11 active online success match
[36] => Total: 1, online 1 OLT(config-interface-epon-0/0)# show ont info 13 all Error: There is no ONT available. OLT(config-interface-epon-0/0)# show ont info 14 all Error: There is no ONT available. OLT(config-interface-epon-0/0)# show ont info 15 all Error: There is no ONT available. OLT(config-interface-epon-0/0)# show ont info 16 all Error: There is no ONT available. OLT(config-interface-epon-0/0)# OLT(config-interface-epon-0/0)#
)
现在我需要过滤这个数组,只提取或存储在另一个数组中只是以
开头的数组0/0 x x xxxxxxxxxx x x x x x
基本上是命令回复,我已经请求了 16 个命令,即设备上的 16 个不同端口,每个端口使用该设备的数据回复连接到该端口的设备
0/0 1 1 00:00:00:00:00 xxxxx xxxxx xxxx xxxx xxxx
下面更新完整的 telnet cmd 回复日志以便更好地理解
OLT(config-interface-epon-0/0)# show ont info 1 all
-----------------------------------------------------------------------------
F/S P ONT MAC Control Run Config Match Desc
ID flag state state state
----------------------------------------------------------------------------
0/0 1 1 AC:4E:91:A6:CF:F5 active online success match VILERMINA-.
-----------------------------------------------------------------------------
Total: 1, online 1
OLT(config-interface-epon-0/0)# show ont info 2 all
-----------------------------------------------------------------------------
F/S P ONT MAC Control Run Config Match Desc
ID flag state state state
----------------------------------------------------------------------------
0/0 2 1 E0:E8:E6:4E:62:58 active online success match CLOVES-NAV.
-----------------------------------------------------------------------------
Total: 1, online 1
OLT(config-interface-epon-0/0)# show ont info 3 all
-----------------------------------------------------------------------------
F/S P ONT MAC Control Run Config Match Desc
ID flag state state state
----------------------------------------------------------------------------
0/0 3 1 E0:67:B3:93:F1:97 active online success match
0/0 3 2 18:C5:8A:B0:19:4F active online success match
-----------------------------------------------------------------------------
Total: 2, online 2
OLT(config-interface-epon-0/0)# show ont info 4 all
-----------------------------------------------------------------------------
F/S P ONT MAC Control Run Config Match Desc
ID flag state state state
----------------------------------------------------------------------------
0/0 4 1 E0:67:B3:A6:45:E1 active online success match Jessica-Si.
0/0 4 2 E0:67:B3:7E:20:05 active online success match onu-id-2
0/0 4 3 E0:67:B3:7D:F8:F9 active online success match onu-id-3
0/0 4 4 E0:E8:E6:4E:69:B8 active online success match onu-id-4
-----------------------------------------------------------------------------
Total: 4, online 4
OLT(config-interface-epon-0/0)# show ont info 5 all
-----------------------------------------------------------------------------
F/S P ONT MAC Control Run Config Match Desc
ID flag state state state
----------------------------------------------------------------------------
0/0 5 1 C4:70:0B:26:B9:38 active online success match
0/0 5 2 E0:67:B3:88:33:E2 active online success match
0/0 5 3 E0:E8:E6:4E:70:B8 active powerdown initial initial
0/0 5 4 C4:70:0B:26:5C:28 active online success match
0/0 5 5 E0:E8:E6:4E:75:C8 active online success match
0/0 5 6 E0:E8:E6:4E:69:C0 active online success match
0/0 5 7 E0:67:B3:93:EF:D5 active online success match
-----------------------------------------------------------------------------
Total: 7, online 6
OLT(config-interface-epon-0/0)# show ont info 6 all
-----------------------------------------------------------------------------
F/S P ONT MAC Control Run Config Match Desc
ID flag state state state
----------------------------------------------------------------------------
0/0 6 1 E0:67:B3:88:3D:57 active online success match
0/0 6 2 E0:E8:E6:4E:71:08 active online success match
0/0 6 3 E0:E8:E6:4E:74:78 active online success match
-----------------------------------------------------------------------------
Total: 3, online 3
OLT(config-interface-epon-0/0)# show ont info 7 all
-----------------------------------------------------------------------------
F/S P ONT MAC Control Run Config Match Desc
ID flag state state state
----------------------------------------------------------------------------
0/0 7 1 00:6D:61:59:D8:48 active online success match
0/0 7 2 E0:E8:E6:4E:55:20 active online success match
-----------------------------------------------------------------------------
Total: 2, online 2
OLT(config-interface-epon-0/0)# show ont info 8 all
-----------------------------------------------------------------------------
F/S P ONT MAC Control Run Config Match Desc
ID flag state state state
----------------------------------------------------------------------------
0/0 8 1 E0:E8:E6:4E:69:E0 active online success match
0/0 8 2 E0:E8:E6:4E:70:08 active online success match
-----------------------------------------------------------------------------
Total: 2, online 2
OLT(config-interface-epon-0/0)# show ont info 9 all
-----------------------------------------------------------------------------
F/S P ONT MAC Control Run Config Match Desc
ID flag state state state
----------------------------------------------------------------------------
0/0 9 1 E0:E8:E6:4E:62:78 active online success match
0/0 9 2 E0:67:B3:A6:58:0F active online success match
0/0 9 3 9C:28:EF:01:AE:99 active online success match
-----------------------------------------------------------------------------
Total: 3, online 3
OLT(config-interface-epon-0/0)# show ont info 10 all
-----------------------------------------------------------------------------
F/S P ONT MAC Control Run Config Match Desc
ID flag state state state
----------------------------------------------------------------------------
0/0 10 1 E0:67:B3:35:CE:9B active online success match
0/0 10 2 E0:67:B3:A6:56:71 active online success match
0/0 10 3 E0:67:B3:A6:25:33 active online success match
-----------------------------------------------------------------------------
Total: 3, online 3
OLT(config-interface-epon-0/0)# show ont info 11 all
-----------------------------------------------------------------------------
F/S P ONT MAC Control Run Config Match Desc
ID flag state state state
----------------------------------------------------------------------------
0/0 11 1 E0:67:B3:7D:F3:E0 active online success match
-----------------------------------------------------------------------------
Total: 1, online 1
OLT(config-interface-epon-0/0)# show ont info 12 all
-----------------------------------------------------------------------------
F/S P ONT MAC Control Run Config Match Desc
ID flag state state state
----------------------------------------------------------------------------
0/0 12 1 E0:67:B3:93:F9:11 active online success match
-----------------------------------------------------------------------------
Total: 1, online 1
OLT(config-interface-epon-0/0)# show ont info 13 all
Error: There is no ONT available.
OLT(config-interface-epon-0/0)# show ont info 14 all
Error: There is no ONT available.
OLT(config-interface-epon-0/0)# show ont info 15 all
Error: There is no ONT available.
OLT(config-interface-epon-0/0)# show ont info 16 all
Error: There is no ONT available.
不要费心使用 preg_split()
将您的文本分成几部分。看起来您正在保存数据,而不管它在哪个部分。
将 preg_match_all()
与可选的最后一个捕获组一起使用。
^
表示“匹配行的开头”,因为 m
模式修饰符。
\h*
表示“匹配零个或多个不代表换行符/换行符的白色space字符”。
\h+\
同上,只是需要出现一次或多次。
最后的水平 space 和可见字符是可选的——这就是带有 ?
的 non-capturing 组所做的。
代码:(Demo)
var_export(
preg_match_all(
'~^\h*(\d+/\d+)\h+(\d+)\h+(\d+)\h+([A-F\d]{2}(?::[A-F\d]{2}){5})\h+(\S+)\h+(\S+)\h+(\S+)\h+(\S+)(?:\h+(\S+))?~m',
$response,
$m,
PREG_SET_ORDER
)
? $m
: 'no matches'
);
查看示例文本和输出的演示。
作为更新,以下数组代码过滤了我需要的数据
$res = array_filter($sections, function ($var) { return (strpos($var, ' 0/0 ') == true);});
输出过滤后的最终结果
Array
(
[2] => 0/0 1 1 AC:4E:91:A6:CF:F5 active online success match VILERMINA-.
[5] => 0/0 2 1 E0:E8:E6:4E:62:58 active online success match CLOVES-NAV.
[8] => 0/0 3 1 E0:67:B3:93:F1:97 active online success match 0/0 3 2 18:C5:8A:B0:19:4F active online success match
[11] => 0/0 4 1 E0:67:B3:A6:45:E1 active online success match Jessica-Si. 0/0 4 2 E0:67:B3:7E:20:05 active online success match onu-id-2 0/0 4 3 E0:67:B3:7D:F8:F9 active online success match onu-id-3 0/0 4 4 E0:E8:E6:4E:69:B8 active online success match onu-id-4
[14] => 0/0 5 1 C4:70:0B:26:B9:38 active online success match 0/0 5 2 E0:67:B3:88:33:E2 active online success match 0/0 5 3 E0:E8:E6:4E:70:B8 active powerdown initial initial 0/0 5 4 C4:70:0B:26:5C:28 active online success match 0/0 5 5 E0:E8:E6:4E:75:C8 active online success match 0/0 5 6 E0:E8:E6:4E:69:C0 active online success match 0/0 5 7 E0:67:B3:93:EF:D5 active online success match
[17] => 0/0 6 1 E0:67:B3:88:3D:57 active online success match 0/0 6 2 E0:E8:E6:4E:71:08 active online success match 0/0 6 3 E0:E8:E6:4E:74:78 active online success match
[20] => 0/0 7 1 00:6D:61:59:D8:48 active online success match 0/0 7 2 E0:E8:E6:4E:55:20 active online success match
[23] => 0/0 8 1 E0:E8:E6:4E:69:E0 active online success match 0/0 8 2 E0:E8:E6:4E:70:08 active online success match
[26] => 0/0 9 1 E0:E8:E6:4E:62:78 active online success match 0/0 9 2 E0:67:B3:A6:58:0F active online success match 0/0 9 3 9C:28:EF:01:AE:99 active online success match
[29] => 0/0 10 1 E0:67:B3:35:CE:9B active online success match 0/0 10 2 E0:67:B3:A6:56:71 active online success match 0/0 10 3 E0:67:B3:A6:25:33 active online success match
[32] => 0/0 11 1 E0:67:B3:7D:F3:E0 active online success match
[35] => 0/0 12 1 E0:67:B3:93:F9:11 active online success match )
现在我只需要拆分其余数据以准备数组转发给查询 mysql
数组需要拆分成新的数组
[8] => 0/0 3 1 E0:67:B3:93:F1:97 active online success match 0/0 3 2 18:C5:8A:B0:19:4F active online success match
[11] => 0/0 4 1 E0:67:B3:A6:45:E1 active online success match Jessica-Si. 0/0 4 2 E0:67:B3:7E:20:05 active online success match onu-id-2 0/0 4 3 E0:67:B3:7D:F8:F9 active online success match onu-id-3 0/0 4 4 E0:E8:E6:4E:69:B8 active online success match onu-id-4
输出
[1] => 0/0 3 1 E0:67:B3:93:F1:97 active online success match
[2] => 0/0 3 2 18:C5:8A:B0:19:4F active online success match
[3] => 0/0 4 1 E0:67:B3:A6:45:E1 active online success match Jessica-Si.
[4] => 0/0 4 2 E0:67:B3:7E:20:05 active online success match onu-id-2
[5] => 0/0 4 3 E0:67:B3:7D:F8:F9 active online success match onu-id-3
[6] =>0/0 4 4 E0:E8:E6:4E:69:B8 active online success match onu-id-4