2016-09-24 86 views
-1
<table border='0' cellspacing='3' width='100%'> 
    <tr> 
     <td width='15%'><font color='#3366FF'><b>Trading Code:</b></font></td> 
     <td width='85%'>RCL</td> 
    </tr> 
    <tr> 
     <td><font color='#3366FF'><b>News Title:</b></font></td> 
     <td>DSENEWS: Withdrawal of Authorized Representative</td> 
    </tr> 
    <tr> 
     <td><font color='#3366FF'><b>News:</b></font></td> 
     <td align='justify'>Withdrawal of Authorized Representative: Royal Capital Ltd., DSE TREC No. 21, has withdrawn one of its Authorized Representatives, Mr. Md. Zikrul Haque, with immediate effect.</td> 
    </tr> 
</table> 
<br> 
<table border='0' cellspacing='3' width='100%'> 
    <tr> 
     <td width='15%'><font color='#3366FF'><b>Trading Code:</b></font></td> 
     <td width='85%'>ISL</td> 
    </tr> 
    <tr> 
     <td><font color='#3366FF'><b>News Title:</b></font></td> 
     <td>DSENEWS: Withdrawal of Authorized Representative</td> 
    </tr> 
    <tr> 
     <td><font color='#3366FF'><b>News:</b></font></td> 
     <td align='justify'>Withdrawal of Authorized Representative: IDLC Securities Ltd., DSE TREC No. 58, has withdrawn one of its Authorized Representatives, Mr. Mohammad Ziaur Rahman, with immediate effect.</td> 
    </tr> 
</table> 

這是我希望從中獲得每張表的交易代碼和標題並將其存儲到我的數據庫中的表格,請幫助我們取消此數據。XPath表格刮取

回答

1
$html="<table border='0' cellspacing='3' width='100%'> 
    <tr> 
     <td width='15%'>Trading Code:</td> 
     <td width='85%'>RCL</td> 
    </tr> 
    <tr> 
     <td><font color='#3366FF'><b>News Title:</b></font></td> 
     <td>DSENEWS: Withdrawal of Authorized Representative</td> 
    </tr> 
    <tr> 
     <td><font color='#3366FF'><b>News:</b></font></td> 
     <td align='justify'>Withdrawal of Authorized Representative: Royal Capital Ltd., DSE TREC No. 21, has withdrawn one of its Authorized Representatives, Mr. Md. Zikrul Haque, with immediate effect.</td> 
    </tr> 
</table> 
<br> 
<table border='0' cellspacing='3' width='100%'> 
    <tr> 
     <td width='15%'><font color='#3366FF'><b>Trading Code:</b></font></td> 
     <td width='85%'>ISL</td> 
    </tr> 
    <tr> 
     <td><font color='#3366FF'><b>News Title:</b></font></td> 
     <td>DSENEWS: Withdrawal of Authorized Representative</td> 
    </tr> 
    <tr> 
     <td><font color='#3366FF'><b>News:</b></font></td> 
     <td align='justify'>Withdrawal of Authorized Representative: IDLC Securities Ltd., DSE TREC No. 58, has withdrawn one of its Authorized Representatives, Mr. Mohammad Ziaur Rahman, with immediate effect.</td> 
    </tr> 
</table>"; 




/* Construct XPath expression to find required data*/ 
$query='//td[contains(. , "Trading Code")]/following-sibling::td|//td[contains(. , "News Title")]/following-sibling::td'; 

/* create the DOMDocument & DOMXPath objects */ 
$dom=new DOMDocument; 
$dom->loadHTML($html); 
$xp=new DOMXPath($dom); 

/* Run the query to find nodes */ 
$col=$xp->query($query); 

/* Process the nodes */ 
if(!empty($col)){ 
    foreach($col as $td){ 
     /* do something with data found */ 
     echo $td->nodeValue; 
    } 
} 

構建XPath表達式(& CSS選擇)時,可以發現的有用的參考here