0

我有以下字符串:

$teststring = " 

                                         Flow Link          Back   Mdix

端口类型 Duplex Speed Neg ctrl State Pressure Mode


fa1 100M-铜线 -- -- -- -- 向下 -- --
fa2 100M- 铜线 -- -- -- -- 向下 -- --
fa3 100M- 铜线 -- -- -- -- 向下 -- - -
fa4 100M-铜线 -- -- -- -- 向下 -- --
fa5 100M- 铜线 -- -- -- -- 向下 -- --
fa6 100M- 铜线 -- -- -- -- 向下 -- --
fa7 100M-Copper -- -- -- -- Down -- --
fa8 100M-Copper -- -- -- -- Down -- --
gi1 1G-Combo-C -- -- -- --向下 - -
gi2 1G-Combo-C Full 100 启用 Off Up Disabled Off

                                      Flow    Link        

Ch Type Duplex Speed Neg control State


Po1 -- -- -- -- -- 不存在 Po2 -- -- -- -- -- 不存在 Po3 -- -- -- -- -- 不存在 Po4 -- -- -- -- --不存在 Po5 -- -- -- -- -- 不存在 Po6 -- -- -- -- -- 不存在 Po7 -- -- -- -- -- 不存在 Po8 -- -- -- -- - 不存在 ” ;

我正在尝试解析每个字段。这是我到目前为止的代码:

$teststring = explode("<BR>", $teststring);     
$vlandetailsArray = array();
foreach ($teststring as $vlandetails) 
{            
  //              port     space    type     space   duplex          speed       space  neg         
  $pattern = '/([a-z0-9]*)(\s*)([a-z0-9\-]*)(\s*)[(Full)|(\-{2})](\s*)[(\-)+|(100)](\s*)[(--)*|(Enabled)](\s*)[(--)*|(Off)]/i';

  if (preg_match($pattern, $vlandetails, $matches)) 
  {  
       echo 'match 0: '.$matches[0].'<br>';  //0 index always returns all matches
      }

这将返回以下内容:

match 0: -------- ---- 
match 0: fa1 100M-Copper -- -- 
match 0: fa2 100M-Copper -- -- 
match 0: fa3 100M-Copper -- -- 
match 0: fa4 100M-Copper -- -- 
match 0: fa5 100M-Copper -- -- 
match 0: fa6 100M-Copper -- -- 
match 0: fa7 100M-Copper -- -- 
match 0: fa8 100M-Copper -- -- 
match 0: gi1 1G-Combo-C -- -- 
match 0: -------- ----
match 0: Po1 -- -- -- 
match 0: Po2 -- -- -- 
match 0: Po3 -- -- --
match 0: Po4 -- -- -- 
match 0: Po5 -- -- -- 
match 0: Po6 -- -- --
match 0: Po7 -- -- -- 
match 0: Po8 -- -- --

我不明白为什么它没有拿起看起来像这样的线:

gi2      1G-Combo-C   Full    100   Enabled  Off  Up          Disabled Off

你能告诉我我错过了什么/做错了吗?
供参考。我还在玩我的正则表达式,所以你会注意到有时我使用模式 (-{2}) 和其他时候 -+ 等。

编辑 1

我已经修改了测试字符串。以前,我有以下代码将任何 CR LF 替换为
.

        $this->_data  = str_replace(chr(10),"<BR>",$this->_data ,$count);//strip New line
        $this->_data  = str_replace(chr(13),'',$this->_data ,$count);//strip carriage return    

对不起,我忽略了这些代码行——我的页面中有太多的“测试”。您现在看到的测试字符串是“原始”内容。我只是将所有内容保存到一个文件中,如下所示:

 $fp = fopen('/var/www/lsm/application/logs/showinterfacesstatus.txt', 'w');
 fwrite($fp, $this->_data);
 fclose($fp);   

其中 $this->_data 包含原始数据。我打开了这个文件并复制了所有内容......然后粘贴到我的 teststring 变量中。
话虽如此,我已经在文本编辑器中分析了文件,我可以看到原始字符串和修改后的字符串之间唯一不同的是它已被剥离所有 CRLF。但如果它有帮助,我已经删除了这个逻辑。我还在文本编辑器中包含了未修改数据的屏幕截图。谢谢。 原始数据

4

2 回答 2

1

试试这个:

#[a-z]{2}\d\s[^\s]+ [a-z\-]+[^\s]+ ([^\s]+) ([^\s]+) ([^\s]+) ([^\s]+) ([^\s]+) ([^\s]+)#si

您必须启用不区分大小写的模式。

另一个,捕获...接口名称?

#[a-z]{2}\d ([^\s]+) [a-z\-]+[^\s]+ ([^\s]+) ([^\s]+) ([^\s]+) ([^\s]+) ([^\s]+) ([^\s]+)#si

完整代码示例:

<?php

$teststring = " 
Flow Link Back Mdix 
Port Type Duplex Speed Neg ctrl State Pressure Mode 
-------- ------------ ------ ----- -------- ---- ----------- -------- ------- 
fa1 100M-Copper -- -- -- -- Down -- -- 
fa2 100M-Copper -- -- -- -- Down -- -- 
fa3 100M-Copper -- -- -- -- Down -- -- 
fa4 100M-Copper -- -- -- -- Down -- -- 
fa5 100M-Copper -- -- -- -- Down -- -- 
fa6 100M-Copper -- -- -- -- Down -- -- 
fa7 100M-Copper -- -- -- -- Down -- -- 
fa8 100M-Copper -- -- -- -- Down -- -- 
gi1 1G-Combo-C -- -- -- -- Down -- -- 
gi2 1G-Combo-C Full 100 Enabled Off Up Disabled Off 

Flow Link 
Ch Type Duplex Speed Neg control State 
-------- ------- ------ ----- -------- ------- ----------- 
Po1 -- -- -- -- -- Not Present 
Po2 -- -- -- -- -- Not Present 
Po3 -- -- -- -- -- Not Present 
Po4 -- -- -- -- -- Not Present 
Po5 -- -- -- -- -- Not Present 
Po6 -- -- -- -- -- Not Present 
Po7 -- -- -- -- -- Not Present 
Po8 -- -- -- -- -- Not Present";

$pattern = '#[a-z]{2}\d ([^\s]+) [a-z\-]+[^\s]+ ([^\s]+) ([^\s]+) ([^\s]+) ([^\s]+) ([^\s]+) ([^\s]+)#si';

preg_match_all($pattern, $teststring, $matches, PREG_SET_ORDER);

foreach ($matches as $match) {
    echo '<pre>';
    var_dump($match);
    echo '</pre>';
}

/* EOF */
于 2012-12-05T21:46:39.040 回答
0

这是有效的正则表达式模式:

$pattern = '/([az]{2}\d)(\s*)([0-9a-z-] )(\s )([az-] )(\s ) ([0-9- ] )(\s )([az-] )(\s )([az-] )(\s )([az-] )(\s )([az-] )(\s )([az- ]*)/一世';

于 2012-12-06T15:08:14.017 回答