2011-04-20 61 views
2

我已經使用file_get_contents()基本上獲取站點的源代碼到單個字符串變量。提取數據PHP字符串

源包含許多行,看起來像這樣: <td align="center"><a href="somewebsite.com/something">12345</a></td>

(和很多行,不看這樣的)。我想提取所有的idnumbers(12345以上)並將它們放入一個數組中。我怎樣才能做到這一點?我假設我想使用某種正則表達式,然後使用preg_match_all()函數,但我不知道如何...

+1

我們必須看到的數據 – Galen 2011-04-20 19:44:06

+0

哦,太好了谷歌,而不是另外一個。 http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454 – Zirak 2011-04-20 19:45:27

回答

1

試試這個:

preg_match('/>[0-9]+<\/a><\/td>/', $str, $matches); 
for($i = 0;$i<sizeof($matches);$i++) 
$values[] = $matches[$i]; 
+0

謝謝!這給了我一個基本的想法,我去 preg_match_all('/ [0-9] + <\/a><\/td> /',$ html,$ matches); return $ matches [0]; 作品perfetly! – faximan 2011-04-20 20:16:08

+0

很高興幫助:)。 – SIFE 2011-04-20 20:28:41

4

不要混淆正則表達式。獲取變量並讓DOM庫爲您完成平凡的任務。看看:http://sourceforge.net/projects/simplehtmldom/

然後你可以像樹一樣遍歷你的HTMl並提取東西。如果你真的想得到時髦,請閱讀xPath。