iT邦幫忙

0

【請問 preg_match_all 抓出所有值???】

php
<tr align=center bgcolor=#FFFFFF>
  <td><font size=2><a href="port_dst.asp?kind=MSER&port_sq=1398346&atmsq=3121">1</a></td>
  <td nowrap><font size=2>1/1/1<br>1-1-1</font></td>
  <td nowrap><font size=2>10GE</font></td>

  
  <td nowrap>
	<font color="blue" size=2 title="系統序號=10982"><a href='/sys/stm/sdh_QryStmSys.asp?stmsq=10982'>PCPC-PCPC                 -<font color='#228B22'>10GE</font>-14</a></font>
	<br><font size='1'>[S]</font> <b><font color=red size=1 title='GroupID=103'>LAG(M)</font></b><br>
	<font color="blue" size=2 title="序號="></font>
  </td>
	   

  <td><font color="#8B0000" size=2>MSER</font></td>
  <td><font color="#388E8E" size=2>ALCATEL-7750SR-12</font></td>
  <td><font size=2>551</font></td>
  <td nowrap><font size=2>1/1/2<br>1-1-2</font></td>
  <td><font size=2></font></td>
  <td><font color="#8B0000" size=2>MSER</font></td>
  <td><font color="#388E8E" size=2>ALCATEL-7750SR-12</font></td>
  <td><font size=2>501</font></td>
  <td nowrap><font size=2>1/1/1<br>1-1-1</font></td>
  <td><font size=2></font></td>
  <td><font size=2>
	0
  </font></td>
  <td><font size=2>
	1
  </font></td>
  <td><font size=2>
	0
  </font></td>
  <td><font size=2>
	2
  </font></td>
  <td><font size=2>
	<a href='http://10.16.3.28/resourcemgmt/facility/facilityShow.do?facsq=10982' target='_blank'>3</a></font>
	
  </td>
  <td><font size=2 color=red> </font></td>
  <td><font size=2 color=red><font color=#4169E1>可用【A】</font></font></td>
  <td><font size=2 color="#00008B"></font></td>
</tr>

這是 table 中的其中一筆資料
我想要把裡面的一些欄位值抓出來(用 preg_match_all 抓出所有的值)
(1)sdh_QryStmSys.asp?stmsq=10982 中的 10982
(2)PCPC-PCPC -10GE-14 中的 PCPC-PCPC-10GE-14
(3)size=2>MSER 中的 MSER
(4)size=2>ALCATEL-7750SR-12 中的 ALCATEL-7750SR-12
(5)size=2>551 中的 551
(6)size=2>1/1/21-1-2 中的 1/1/2
(7)size=2>MSER 中的 MSER
(8)size=2>ALCATEL-7750SR-12 中的 ALCATEL-7750SR-12
(9)size=2>501 中的 501
(10)size=2>1/1/11-1-1 中的 1/1/1
(11) 中的 0
0

(12) 中的 1
1

(13) 中的 0
0

(14) 中的 2
2

最後抓出這筆資料所有欄位的值
10982=>PCPC-PCPC-10GE-14=>MSER=>ALCATEL-7750SR-12=>551=>1/1/2=>MSER=>ALCATEL-7750SR-12=>501=>1/1/1=>0=>1=>0=>2

我只有寫出
10982 和 PCPC-PCPC-10GE-14(尚未加工)
接下來的資料在別行
不知道要怎麼寫

preg_match_all('#sdh_QryStmSys.asp\?stmsq=(\d+)\'>(.*)<\/a#',$f,$matches);
//$matches[1] 是 10982
//$matches[2] 是 PCPC-PCPC                 -<font color='#228B22'>10GE</font>-14(可再加工得到 PCPC-PCPC-10GE-14)

請問接下來怎麼做呢??

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中
1
weiclin
iT邦高手 4 級 ‧ 2016-07-24 02:27:46
最佳解答

其實你都條列出來了,而且我覺的還蠻規律的 xD

寫個範例給你參考參考:

<?php

$html = file_get_contents("data.txt"); # 讀取你的範例 html

$datas = preg_match('@
    <tr
        .*stmsq=(\d+?)      # (1)sdh_QryStmSys.asp?stmsq=10982 中的 10982
        .*>(.*)</a>         # (2)PCPC-PCPC -10GE-14 中的 PCPC-PCPC-10GE-14, 需再加工
        .*size=2>(.*)<      # (3)size=2>MSER 中的 MSER
        .*size=2>(.*)<      # (4)size=2>ALCATEL-7750SR-12 中的 ALCATEL-7750SR-12
        .*size=2>(.*)<      # (5)size=2>551 中的 551
        .*size=2>(.*)<      # (6)size=2>1/1/21-1-2 中的 1/1/2
        .*size=2>.*<        # skip a td
        .*size=2>(.*)<      # (7)size=2>MSER 中的 MSER 
        .*size=2>(.*)<      # (8)size=2>ALCATEL-7750SR-12 中的 ALCATEL-7750SR-12
        .*size=2>(.*)<      # (9)size=2>501 中的 501
        .*size=2>(.*)<      # (10)size=2>1/1/11-1-1 中的 1/1/1
        .*size=2>.*<        # skip a td
        .*size=2>.*(\d+)    # (11) 中的 0 0
        .*size=2>.*(\d+)    # (12) 中的 1 1 
        .*size=2>.*(\d+)    # (13) 中的 0 0 
        .*size=2>.*(\d+)    # (14) 中的 2 2
        .*
    </tr>
@msxU', $html, $matches);

# 處理 PCPC-PCPC-10GE-14 中多餘的字元
$matches[2] = preg_replace('#\s|<.*?>#', "", $matches[2]);

for ($i = 1; $i <= 14; ++$i) {
    echo "($i) ", $matches[$i], "\n";
}

輸出是:

(1) 10982
(2) PCPC-PCPC-10GE-14
(3) MSER
(4) ALCATEL-7750SR-12
(5) 551
(6) 1/1/2
(7) MSER
(8) ALCATEL-7750SR-12
(9) 501
(10) 1/1/1
(11) 0
(12) 1
(13) 0
(14) 2
0
fillano
iT邦超人 1 級 ‧ 2016-07-23 22:48:27

我是覺得都用pref_match_all來抓,而沒有比較好的pattern時,困難度很大,建議你換個角度想。

例如用幾個步驟處理資料:

  1. 抓出table
  2. 把table的內容切成row
  3. 把row切成column
  4. 最後根據column的特性來抓內容,有些只要你去掉html tag就是你要的東西,不一定要用regular expression處理

參考一下。

andyto202 iT邦研究生 4 級 ‧ 2016-07-23 23:19:14 檢舉

謝謝 fillano 大
其實我之前就有寫出來了
不過我覺得我是用笨方法
我是先存成陣列
然後用算的http://ithelp.ithome.com.tw/articles/create
例如先找一行要的為基準行
例如
10982 那行
+ 6 就是 A端設備 MSER
+ 7 就是 廠牌型號 ALCATEL-7750SR-12
以此類推
可是這樣子感覺有點 low 啊

fillano iT邦超人 1 級 ‧ 2016-07-23 23:52:24 檢舉

了解XD,我只是覺得裡面有些資料用regular expression抓有點困難...

0
一級屠豬士
iT邦大師 1 級 ‧ 2016-07-25 09:23:30

自給自足,豐衣足食.

我要發表回答

立即登入回答