我有這樣一套具有<tr>
標籤更TD和TR標籤以及HTML表格 <table width=250 border=0 cellspacing=0 cellpadding=0 bgcolor=#F9F400>
的。如何使用simplehtmldom來分析和尋找一個表
,我有這個PHP表達echo $html->find(''table td[bgcolor=#F9F400]');
但沒有什麼呼應,有沒有記錄的錯誤,這是錯誤的方法來做到這一點?我想按原樣顯示整個表格。
我有這樣一套具有<tr>
標籤更TD和TR標籤以及HTML表格 <table width=250 border=0 cellspacing=0 cellpadding=0 bgcolor=#F9F400>
的。如何使用simplehtmldom來分析和尋找一個表
,我有這個PHP表達echo $html->find(''table td[bgcolor=#F9F400]');
但沒有什麼呼應,有沒有記錄的錯誤,這是錯誤的方法來做到這一點?我想按原樣顯示整個表格。
從給出的HTML:
<table width=250 border=0 cellspacing=0 cellpadding=0 bgcolor=#F9F400>
您需要選擇其bgcolor
是#F9F400
表。您當前正在選擇具有背景顏色的td
元素。要獲得該表,請嘗試:
$table = $html->find('table[bgcolor=#F9F400]', 0);
的0
表明您希望第一個結果,否則你將得到一個數組返回。那麼你可以在echo
這個表中,它會自動將對象轉換成一個字符串;
echo $table;
如果你想獲得的所有表內td
元素:
$tds = $table->find('td');
注意這將返回一個數組,所以你需要通過他們循環打印他們的內容。類似你寫的,你可以做到這一點,如:
// get all tds of table with bgcolor #F9F400
$tds = $html->find('table[bgcolor=#F9F400] td');
foreach ($tds as $td) {
// do what you like with the td
echo $td;
}
建議第三方替代[SimpleHtmlDom(http://simplehtmldom.sourceforge.net/)實際使用[DOM(HTTP:// PHP .net/manual/en/book.dom.php)而不是字符串分析:[phpQuery](http://code.google.com/p/phpquery/),[Zend_Dom](http://framework.zend。 com/manual/en/zend.dom.html),[QueryPath](http://querypath.org/)和[FluentDom](http://www.fluentdom.org)。 – Gordon