解决方案 »
- 毕业才开始接触程序,公司带我的师傅给了我出了一道关于排序的题,我写的东西总是没输出,麻烦各位进来帮我看一看。
- 怎么写查找一段HTML代码中<a href=""></a>所有超链接地址的正则表达式?
- 我在PHPmyadmin 里直接用SQL语句插入的数据, 取出来显示在网页上是乱码, 为什么
- php5.20 xp iis5.1 配置问题
- 招PHP工程师,地点:武汉
- 有办法判断一个网页是静态还是动态么?
- 正则分析效率虽然高,但是cpu占用会不会也很高?
- 一个小问题,为何$_POST影响不了$_SESSION?
- 数据列表如何用颜色来标记??有没有更好的方法??
- 有没有做过招行的网上支付的兄弟,请问怎么做IP验证?
- 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个?