[英]Problem with PHP regular expressions
我只是似乎無法弄清楚如何使用正則表達式從頁面中提取整個表。
這是我的PHP:
$printable = file_get_contents('http://entertainment.soundboxaudio.com/testplaylist.htm');
$array = array();
preg_match( '/<TABLE>(.*?)<\/TABLE>/si', $printable, $array ) ;
$findit = "$array[1]";
echo("$findit");
任何幫助,將不勝感激,
謝謝!
這里我們再次嘗試...不要使用正則表達式提取HTML。 HTML不是常規語言,因此無法使用正則表達式可靠地進行分析。 改用DOM。
$printable = file_get_conttents('...');
$dom = new DOMDocument;
$dom->loadHTML($printable);
$xpath = new DOMXpath($dom);
$tables = $xpath->query("//table");
$table_html = array();
foreach($tables as $table) { // workaround for PHP DOM not support innerHTML
$temp = new DOMDocument;
$temp->appendChild($temp->importNode($table, true));
$table_html[] = trim($temp->saveHTML());
}
同樣,您要回顯的周圍變量只是浪費字符串操作
echo $x
echo "$x";
工作方式相同,除了引用版本浪費了一些cpu產生的臨時字符串只會再次被丟棄。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.