php正则表达式查找html内容

html如下:
<div class="c1">aa</div>
<a class="c2" href="任意网址">1111</a>
<a class="c2" href="任意网址">2222</a>
<a class="c2" href="任意网址">3333</a>
<div class="c1">bb</div>
<a class="c2" href="任意网址">1111</a>
<a class="c2" href="任意网址">2222</a>
<a class="c2" href="任意网址">3333</a>
<a class="c2" href="任意网址">444</a>
<div class="c1">cc</div>
<a class="c2" href="任意网址">55</a>
<a class="c2" href="任意网址">66</a>

.
.
.
我想读取html片断是<div>和后面跟着的几个<a>如下:
$matches[0]:

<div class="c1">aa</div>
<a class="c2" href="任意网址">1111</a>
<a class="c2" href="任意网址">2222</a>
<a class="c2" href="任意网址">3333</a>
$matches[1]:
<div class="c1">bb</div>
<a class="c2" href="任意网址">1111</a>
<a class="c2" href="任意网址">2222</a>
<a class="c2" href="任意网址">3333</a>
<a class="c2" href="任意网址">444</a>
$matches[2]:
<div class="c1">cc</div>
<a class="c2" href="任意网址">55</a>
<a class="c2" href="任意网址">66</a>
通过正则如何得到$matches数组???

php正则表达式查找html内容的方法:
preg_match('/<p align=\"center\"><big><strong>(.*?)<\/strong><\/big><\/p>/',$str,$result);
代码解释:
$str就是上面的html里面的内容;
$result就是匹配到的字符串,可以print_r($result);看看里面就有你要的结果,或者直接echo $result[1];
就是“在LINUX下配置MYSQL、PHP和JSP”这几个字符了。
温馨提示:答案为网友推荐,仅供参考
第1个回答  2014-07-12
<div[\s\S]+?(?=<div|$)
第2个回答  2014-07-12
preg_match_all('/<div\s+[^>]*>[^<>]*<\/div>(?:\s*<a\s+[^<>]*>[^<>]*<\/a>)*/',$htmltext,$matches,PREG_SET_ORDER);追问

你好,如果每个之间有 ,如何写?

追答

preg_match_all('/]*>[^]*(?:\s*]*>(?:.(?!\/a>))*)+/s',$htmltext,$matches,PREG_SET_ORDER);

本回答被提问者采纳

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网