-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 & 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/>';
錯誤:未定義的偏移。請幫我
在使用它的值之前,你應該首先檢查是否匹配。'if(isset($ match [0]))' – Chief
我做了。它不匹配。 –
[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來得到你需要的信息 –