不要使用正則表達式對這類事情,儘量DOM解析器如SimpleHTMLDom。
<?php
require_once('simple_html_dom.php');
echo file_get_html('http://www.google.com/finance')->find('#markets', 0);
?>
是啊......就這麼簡單:)
編輯:
在回答您的意見,不料SimpleHTMLDom迷死:
<?php
require_once('simple_html_dom.php');
$html = file_get_contents('http://www.google.com/finance');
$tidy = tidy_parse_string($html);
$tidy->cleanRepair();
$html = str_get_html((string)$tidy);
foreach($html->find('#markets .quotes', 0)->find('tr') as $line) {
printf("%s - %s - %s %s<br />",
$line->find('.symbol a', 0)->innertext,
$line->find('.price span', 0)->innertext,
$line->find('.change span', 0)->innertext,
$line->find('.change span', 1)->innertext);
}
?>
呀,我不得不對該頁面使用Tidy ......我不知道Google僱傭誰來做HTML,但這絕對是可怕的。未關閉的TD的,具有相同的ID等多種元素...解析器哽咽那些:(
什麼的print_r的輸出也這傢伙有一些合理的答案,你想做什麼:http://stackoverflow.com/questions/1732348/regex-match-open-tags-除-xhtml-self-contained-tags/1732454#1732454 – halfdan 2011-04-21 08:32:21
請確保允許您從URL讀取文件(安全風險很低的系統) – knittl 2011-04-21 08:33:20
試試'echo $ div_array [0]' – 2011-04-21 08:33:36