2015-08-03 18 views
-1

我解析一個html頁面,我想要定期更改的某個值。但我不知道如何實現它。如何使用正則表達式在php中檢索文本(html)數據

<table class="priceTable"> 
<tr> 
    <th colspan="3">Generic Gold Price By Carat/Karat - Today - Today <br> 
    Mon, Aug 3rd, 2015<br>Gold Price Today Per Gram - Current Gold Price in Indian Rupees</th> 
</tr> 

<tr>  
    <th>22 Karat Today</th> 
    <th>22 Karat Yesterday</th> 
    <th>Price Change</th> 

</tr> 
<tr>  
    <td>1g = Rs. 2377.00</td> 
    <td>1g = Rs. 2377.00</td> 
    <td> 

      0  <img SRC="images/green.gif" alt="India Gold Rate Price Difference Today"> 
     </td> 
</tr> 
<tr>  
    <th>24 Karat (Pure Gold) Today</th> 
    <th>24 Karat (Pure Gold) Yesterday</th> 
    <th>Price Change</th> 
</tr> 
<tr>  
    <td>1g = Rs. 2541.00</td> 
    <td>1g = Rs. 2541.00</td> 
    <td> 
      0  <img SRC="images/green.gif" alt="India Gold Rate Price Difference Today"> 
     </td> 
</tr> 
</table> 

我需要1克22克拉今天和24克拉今天的價值。 我的代碼

$regex='/<table class="priceTable"> 
<tbody><tr> 
    <th colspan="3">Generic Gold Price By Carat\/Karat - Today - Today <br> 
    Mon, Aug 3rd, 2015<br>Gold Price Today Per Gram - Current Gold Price in Indian Rupees<\/th> 
<\/tr> 

<tr>  
    <th>22 Karat Today<\/th> 
    <th>22 Karat Yesterday<\/th>  
    <th>Price Change<\/th> 

<\/tr> 
<tr>  
    <td>(^[a-zA-Z0-9_.-]*$)<\/td>/'; 



//$regex='/<tr> 
// <td>MUMBAI<br><br><a class="highlightlink" href="http:\/\/mumbai.indiagoldrate.com">Gold Rates in <br>Mumbai - More Info &amp; archives<\/a> 
// 
// <br><br><!-- <A class="highlightlink" HREF="mumbai-gold-rate-on-2015-08-03.htm">Gold & Silver Rates in <BR>Mumbai - on 3-Aug-2015<\/A> --> 
// <\/td> 
// <td><table class="innerTable"> 
// <tbody><tr> 
//  <td>([\w\W]*?)<\/td>/'; 
preg_match($regex,$data,$match); 
$line = $match[0]; 
echo $line; 
echo '<br/>'; 

錯誤:未定義的偏移。請幫我

+0

在使用它的值之前,你應該首先檢查是否匹配。'if(isset($ match [0]))' – Chief

+0

我做了。它不匹配。 –

+3

[regex + markup does not play well](http://stackoverflow.com/a/1732454/1230836):標記最好通過_parsing it_處理(使用['SimpleXMLElement'](http:// php .net/simplexmlelement)或['DOMDocument'](http://php.net/DOMDocument)),然後設置關於有條件地遍歷/查詢DOM來得到你需要的信息 –

回答

0

從你的正則表達式判斷,你試圖獲得第一個td標記在表中的每一行中的節點值,其中class等於priceTable

我建議使用DOMDocumentDOMXPath

$dom = new DOMDocument('1.0', 'UTF-8'); 
$dom->loadHTML($html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD); 

$xpath = new DOMXPath($dom); 
$tds = $xpath->query("//table[@class='priceTable']/tr/td[1]");//th[contains(., 'Karat')] 

foreach($tds as $td) { 
    echo $td->nodeValue . "\n"; 
} 

IDEONE demo

如果你使用你提供的輸入您將獲得2個值:1g = Rs. 2377.001g = Rs. 2541.00

相關問題