php使用curl和preg_match_all [英] php using curl and preg_match_all
问题描述
我觉得我错过了一些东西。我使用下面的代码从表中拉出一些数字。它看起来很简单,我似乎不能得到任何东西打印。我把我的代码和下面的表的例子。请帮我找到我的错误。我希望它只打印每个单元格中的数字。
I feel like I am missing something. I am using the following code to pull some numbers from a table. As simple as it looks, I cannot seem to get anything to print. I am placing my code and an example of the table below. Please help me find my error. I want it to print out only the numbers in each cell.
//gets the site
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://site.org');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
//parse the data
preg_match_all('/<td align=right>(\d+?)</td>/', $response, $matches2);
//prints the parsed data
print_r($matches2[0]);
以下是表格的示例。
<center><table border=1><tr><th align=left>Address</th><th width=50>0</th><th width=50>1</th><th width=50>2</th><th width=50>3</th><th width=50>4</th><th width=50>5</th><th width=50>6</th><th width=50>7</th><th width=50>8</th><th width=50>9</th></tr><tr><td>N7:0</td>
<td align=right>1</td>
<td align=right>1</td>
<td align=right>1</td>
<td align=right>99</td>
<td align=right>0</td>
<td align=right>0</td>
<td align=right>0</td>
<td align=right>0</td>
<td align=right>0</td>
<td align=right>0</td>
</tr><tr><td>N7:10</td>
<td align=right>0</td>
<td align=right>7300</td>
<td align=right>16400</td>
<td align=right>3300</td>
<td align=right>2200</td>
<td align=right>6100</td>
<td align=right>28000</td>
<td align=right>18000</td>
<td align=right>0</td>
<td align=right>0</td>
</tr></table></center><hr width=25% align=center>
推荐答案
PHP错误报告应该暗示。
我强烈建议在开发期间将error_reporting设置为E_ALL和display_errors设置为on。这将给你一个提示,为什么你没有得到任何结果:
The PHP-Error-Reporting should have given you a hint. I strongly advice to set error_reporting to E_ALL and display_errors to "on" during development.This would have given you a hint, as to why you don't get any results:
PHP Warning: preg_match_all(): Unknown modifier 't'
因此,你应该在正则表达式中的斜杠中添加一个转义。
So you should add an escape to the slash inside your regex, because, you use it as delimiter.
preg_match_all('/<td align=right>(\d+?)<\/td>/', $response, $matches2);
和以往一样,我建议使用另一个分隔符,从而保持正则表达式更加可读。我通常选择〜。其格式如下:
As always, I would suggest, using another delimiter and thus keep your regex more readable. I normally choose "~". This would look like:
preg_match_all('~<td align=right>(\d+?)</td>~', $response, $matches2);
这篇关于php使用curl和preg_match_all的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!