我有一些代码可以提取一堆数据并将结果存储到变量中。 当我尝试在其上运行reg表达式时,它无法找到匹配项。但是,如果我通过执行echo语句将变量转储到屏幕,然后将屏幕上的所有数据复制到变量中并在新变量上运行正则表达式,那么它就能找到80多个匹配项。
以下是我目前使用的代码:
require 'HPVC.php';
$hp = new HPVC('10.14.3.23', '123', '');
echo "<br>Attempting connection";
$hp->connect();
echo "<br>connection succeeded";
$data = $hp->showPSAll();
echo "<br>closing connection...";
$hp->close();
echo "<br>connection closed.";
print_r($data);
$pattern = '/(\w+)(\s*)(\w+\/\w+|\s+)(\s*)(\|)(\s*)(\w+)(\s*)(\w+)(\s*)(\w+|\s+)(\s*)(\w+)(\s*)(\w+)(\s*)(\w+)(\s*)(\d)/i';
echo '<br>Number of matches: '.preg_match_all($pattern, $data, $matches, PREG_PATTERN_ORDER);
这是print_r部分给出的内容:(真的很长!)
connection closed. [24;1H [24;16H [24;1H [?25h [24;16H [24;16Hshow inter [24;16H [?25h [24;26H [24;26Hfaces brie [24;26H [?25h [24;36H [24;36Hf [24;36H [?25h [24;37H [24;0H E [24;1H [24;37H [24;1H [2K [24;1H [?25h [24;1H [1;24r [24;1H Status and Counters - Port Status | Intrusion MDI Flow Bcast Port Type | Alert Enabled Status Mode Mode Ctrl Limit ------ --------- + --------- ------- ------ ---------- ---- ---- ----- A1 100/1000T | No Yes Down 1000FDx Auto off 0 A2 100/1000T | No Yes Down 1000FDx Auto off 0 A3 100/1000T | No Yes Down 1000FDx Auto off 0 A4 100/1000T | No Yes Down 1000FDx Auto off 0 A5 100/1000T | No Yes Down 1000FDx Auto off 0 A6 100/1000T | No Yes Down 1000FDx Auto off 0 A7 100/1000T | No Yes Down 1000FDx Auto off 0 A8 100/1000T | No Yes Down 1000FDx Auto off 0 A9 100/1000T | No Yes Down 1000FDx Auto off 0 A10 100/1000T | No Yes Down 1000FDx Auto off 0 A11 100/1000T | No Yes Down 1000FDx Auto off 0 A12 100/1000T | No Yes Down 1000FDx Auto off 0 A13 100/1000T | No Yes Down 1000FDx Auto off 0 A14 100/1000T | No Yes Down 1000FDx Auto off 0 A15 100/1000T | No Yes Down 1000FDx Auto off 0 A16 100/1000T | No Yes Down 1000FDx Auto off 0 A17 100/1000T | No Yes Down 1000FDx Auto off 0 [24;1H [2K [24;1H [1;24r [24;1H A18 100/1000T | No Yes Down 1000FDx Auto off 0 A19 100/1000T | No Yes Down 1000FDx Auto off 0 A20 100/1000T | No Yes Down 1000FDx Auto off 0 A21 100/1000T | No Yes Down 1000FDx Auto off 0 A22 100/1000T | No Yes Down 1000FDx Auto off 0 A23 100/1000T | No Yes Down 1000FDx Auto off 0 A24 100/1000T | No Yes Down 1000FDx Auto off 0 B1 100/1000T | No Yes Down 1000FDx Auto off 0 B2 100/1000T | No Yes Down 1000FDx Auto off 0 B3 100/1000T | No Yes Down 1000FDx Auto off 0 B4 100/1000T | No Yes Down 1000FDx Auto off 0 B5 100/1000T | No Yes Down 1000FDx Auto off 0 B6 100/1000T | No Yes
有人可以协助解决这个问题吗?
答案 0 :(得分:2)
检查您的原始数据。您可能要复制的内容与$ data中的内容略有不同。例如,非白色空格字符显示为空白区域,然后被复制为空白区域。您可能必须使用二进制写入将$ data转储到文件,然后使用十六进制编辑器查看字符。
我过去曾经遇到类似的问题。
答案 1 :(得分:1)
将最后一行替换为:
echo '<br>Number of matches: '.preg_match_all($pattern, print_r($data, true), $matches, PREG_PATTERN_ORDER);
(print_r
的第二个参数为true
,返回输出而不是将其打印到屏幕。)
如果这解决了您的问题,那么,正如其他人所评论的那样,您的问题是$data
不是字符串,而是一个数组,或者toString
print_r
编辑的对象{{1}} 1}}编