求一个正则表达式 本帖最后由 setimouse 于 2011-02-14 18:26:46 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 <?php $str = '<table class="sp"> <tr> <td>td1</td> <td>td2</td> </tr> <tr> <td>td3</td> <td>td4</td> </tr></table>';$regx = "/<td>(.*)<\/td>/isU";if (preg_match_all($regx,$str,$out)) { echo "OK";}else{ echo "err";}print_r($out);如果比较少,可以用jquery哦。。 不是简单地匹配td中的内容,而是匹配table[class=sp]这样的表中的td内容。 $html='<table class="sp"> <tr> <td>td1</td> <td>td2</td> </tr> <tr> <td>td3</td> <td>td4</td> </tr></table>';preg_match('@<table class="sp">(.*?)</table>@is',$html,$out);preg_match_all('/<td>(.*?)<\/td>/is',$out[1],$out);print_r($out[1]); 建议用simple_html_dom这个类来解析,因为你还可能要匹配其他的。谷歌搜索“PHP simple_html_dom” preg_match_all('/<table class="sp">(.*?)<\/table>/s', $html, $matches1);preg_match_all('/<td>(.*?)<\/td>/', implode($matches1[1]), $matches2);var_dump($matches2[1]); 本采集规则 只要是table 包含有 class="sp" 就能被匹配到例如:<table border="0" class="sp" height="50" ><table class="sp" width="20" height="50" ><table border="0" style="color:#FF0000" class="sp" ><table class="sp">等等不论大小写 都可以匹配到。$html = <<<eof<table border="0" ds class="sp" width="600" height="50"> <tr> <td>td1</td> <td>td2</td> </tr> <tr> <td>td3</td> <td>td4</td> </tr></table>eof;preg_match_all('/<table[^>]*(?:class="sp")[^>]*>(.*?)<\/table>/is', $html, $match);preg_match_all('/<td>(.*?)<\/td>/is', implode($match[1]), $matches);print_r($matches[1]); 分层匹配是比较合适的方法,即先匹配出来class="sp"的table, 再对其中的<td>标签进行匹配即可,试一下楼上的吧 不能用jquery,也不用simplehtmldom能否只用一个正则来解析,而不用2个? preg_match_all('/\<table\s+class=\"sp\"\>(.*)\<\/table\>/',$str,$gcz); 仿qq分页的代码 用phpmyadmin 导入CSV文件 中文是乱码 php 为什么什么也没有显示? PHP生成HTML静态的CMS例子 写一个PHP文件,自动循环创建MYSQL表,20分相送 mysql导入时出现乱码 PHP里面date("Y-m-d") 表示今天。怎么表示明天? zh_cn,gb2312,cp936,euc-cn的区别。 一个关于PHP与JS的AJAX传输问题……请问有无大神遇到过这种情况? php中session和cookie的问题 curl为什么只产生一行cookie
<?php
$str = '<table class="sp">
<tr>
<td>td1</td>
<td>td2</td>
</tr>
<tr>
<td>td3</td>
<td>td4</td>
</tr>
</table>
';
$regx = "/<td>(.*)<\/td>/isU";
if (preg_match_all($regx,$str,$out)) {
echo "OK";
}else{
echo "err";
}
print_r($out);
如果比较少,可以用jquery哦。。
不是简单地匹配td中的内容,而是匹配table[class=sp]这样的表中的td内容。
<tr>
<td>td1</td>
<td>td2</td>
</tr>
<tr>
<td>td3</td>
<td>td4</td>
</tr>
</table>
';
preg_match('@<table class="sp">(.*?)</table>@is',$html,$out);
preg_match_all('/<td>(.*?)<\/td>/is',$out[1],$out);
print_r($out[1]);
建议用simple_html_dom这个类来解析,因为你还可能要匹配其他的。
谷歌搜索“PHP simple_html_dom”
preg_match_all('/<td>(.*?)<\/td>/', implode($matches1[1]), $matches2);
var_dump($matches2[1]);
例如:
<table border="0" class="sp" height="50" >
<table class="sp" width="20" height="50" >
<table border="0" style="color:#FF0000" class="sp" >
<table class="sp">
等等不论大小写 都可以匹配到。$html = <<<eof
<table border="0" ds class="sp" width="600" height="50">
<tr>
<td>td1</td>
<td>td2</td>
</tr>
<tr>
<td>td3</td>
<td>td4</td>
</tr>
</table>
eof;preg_match_all('/<table[^>]*(?:class="sp")[^>]*>(.*?)<\/table>/is', $html, $match);
preg_match_all('/<td>(.*?)<\/td>/is', implode($match[1]), $matches);
print_r($matches[1]);
分层匹配是比较合适的方法,即先匹配出来class="sp"的table, 再对其中的<td>标签进行匹配即可,试一下楼上的吧
能否只用一个正则来解析,而不用2个?