preg_match 从 table 检索数据的问题
preg_match issue to retrieve data from table
<h3 style="border-bottom: 3px solid #CCC;" class="margint15 marginb15">Headlines</h3>
<table cellpadding="0" cellspacing="0" border="0" class="nc" width="100%">
<tr>
<th class="left" colspan="2">Latest Headlines</th>
</tr>
<tr>
<td class="left" width="620"> <a href="/blogs/rhb/79680.jsp" style="color:#06a;">Trading Stocks - 10
July 2015 - Globetronics | A&M | Salcon | Comintel | Homeritz |
MMSV</a> </td>
</tr>
</table>
我想从 class="nc" 标签“”中提取数据,直到标签“”结束。如何为 preg_match 编写模式?
真的,这已经在这里讨论了一千遍了,better not use some regular expression to grab html tags (there may be cases in which in works quite well though). For the sake of the christmas spirit, here's an example for your purpose (scraping financial data of a site that is not yours ;-)) Consider using an XML parser 相反:
<?php
$str='<container>
<h3 style="border-bottom: 3px solid #CCC;" class="margint15
marginb15">Headlines</h3> <table cellpadding="0" cellspacing="0"
border="0" class="nc" width="100%"> <tr><th class="left"
colspan="2">Latest Headlines</th></tr> <tr><td class="left" width="620"> <a
href="/blogs/rhb/79680.jsp" style="color:#06a;">Trading Stocks - 10
July 2015 - Globetronics | A&M | Salcon | Comintel | Homeritz |
MMSV</a> </td></tr></table>
</container>';
$xml = simplexml_load_string($str);
print_r($xml);
// now you can loop over the table rows with
foreach ($xml->table->tr as $row) {
// do whatever you want with it
// child elements can be accessed likewise
}
?>
提示: 显然,container
标签是我编的,你的情况很可能是html
。
附录:如Scuzzy points out, make yourself familiar with xpath (here's a good starting point),组合起来极其强大
你应该这样做:
$str = '<h3 style="border-bottom: 3px solid #CCC;" class="margint15 marginb15">Headlines</h3><table cellpadding="0" cellspacing="0" border="0" class="nc" width="100%"> <tr><th class="left" colspan="2">Latest Headlines</th></tr> <tr><td class="left" width="620"> <a href="/blogs/rhb/79680.jsp" style="color:#06a;">Trading Stocks - 10 July 2015 - Globetronics | A&M | Salcon | Comintel | Homeritz | MMSV</a> </td></tr></table>';
preg_match_all('/<table.*?>(.*?)<\/table>/si', $str, $matches);
echo "<pre>";
print_r( strip_tags($matches[1][0]) );
die();
谢谢!
<h3 style="border-bottom: 3px solid #CCC;" class="margint15 marginb15">Headlines</h3>
<table cellpadding="0" cellspacing="0" border="0" class="nc" width="100%">
<tr>
<th class="left" colspan="2">Latest Headlines</th>
</tr>
<tr>
<td class="left" width="620"> <a href="/blogs/rhb/79680.jsp" style="color:#06a;">Trading Stocks - 10
July 2015 - Globetronics | A&M | Salcon | Comintel | Homeritz |
MMSV</a> </td>
</tr>
</table>
我想从 class="nc" 标签“”中提取数据,直到标签“”结束。如何为 preg_match 编写模式?
真的,这已经在这里讨论了一千遍了,better not use some regular expression to grab html tags (there may be cases in which in works quite well though). For the sake of the christmas spirit, here's an example for your purpose (scraping financial data of a site that is not yours ;-)) Consider using an XML parser 相反:
<?php
$str='<container>
<h3 style="border-bottom: 3px solid #CCC;" class="margint15
marginb15">Headlines</h3> <table cellpadding="0" cellspacing="0"
border="0" class="nc" width="100%"> <tr><th class="left"
colspan="2">Latest Headlines</th></tr> <tr><td class="left" width="620"> <a
href="/blogs/rhb/79680.jsp" style="color:#06a;">Trading Stocks - 10
July 2015 - Globetronics | A&M | Salcon | Comintel | Homeritz |
MMSV</a> </td></tr></table>
</container>';
$xml = simplexml_load_string($str);
print_r($xml);
// now you can loop over the table rows with
foreach ($xml->table->tr as $row) {
// do whatever you want with it
// child elements can be accessed likewise
}
?>
提示: 显然,container
标签是我编的,你的情况很可能是html
。
附录:如Scuzzy points out, make yourself familiar with xpath (here's a good starting point),组合起来极其强大
你应该这样做:
$str = '<h3 style="border-bottom: 3px solid #CCC;" class="margint15 marginb15">Headlines</h3><table cellpadding="0" cellspacing="0" border="0" class="nc" width="100%"> <tr><th class="left" colspan="2">Latest Headlines</th></tr> <tr><td class="left" width="620"> <a href="/blogs/rhb/79680.jsp" style="color:#06a;">Trading Stocks - 10 July 2015 - Globetronics | A&M | Salcon | Comintel | Homeritz | MMSV</a> </td></tr></table>';
preg_match_all('/<table.*?>(.*?)<\/table>/si', $str, $matches);
echo "<pre>";
print_r( strip_tags($matches[1][0]) );
die();
谢谢!