开发者社区> 问答> 正文

php中如何用preg_match_all匹配字符串呢?

 <div class="Wrapper">
<div class="colImg">
<a href="http://store.shopping.yahoo.co.jp/matsunami/fcdm3.html"></a>
</div>
<div class="wrCol cf">
<div class="colText">

</div>
<div class="colPrice">
<p class="price"><em>4,603</em></p>
</div>

<div class="colStore">

</div>
</div>
<div class="itemMatch"><a href="http://topics.shopping.yahoo.co.jp/notice/archives/post_8.html">123</a></div>
</div>

我想要获取
<div class="Wrapper"></div>
里面的整个内容(包含了6个div,也可能是7个div,所以包含的div个数可能不定),即:

<div class="colImg">
<a href="http://store.shopping.yahoo.co.jp/matsunami/fcdm3.html"></a>
</div>
<div class="wrCol cf">
<div class="colText">

</div>
<div class="colPrice">
<p class="price"><em>4,603</em></p>
</div>

<div class="colStore">

</div>
</div>
<div class="itemMatch"><a href="http://topics.shopping.yahoo.co.jp/notice/archives/post_8.html">123</a></div>

如何用preg_match_all获取如上字符串,或者用其他方法来实现?

展开
收起
小旋风柴进 2016-03-06 15:04:41 2617 0
1 条回答
写回答
取消 提交回答
  • preg_match_all($reg,$str.$data)
    $reg 为你匹配的正则表达式
    $str 为要匹配的字符串
    $data 为匹配到的数组
    首先$str = file_get_contents($filename)将文件读入字符串
    然后正则`$reg = '/^
    (.*)

    $/';`
    然后preg_match_all($reg,$str,$data)
    打印下$data看看 这种多层嵌套不是太容易匹配 多改改正则即可
    2019-07-17 18:54:39
    赞同 展开评论 打赏
问答分类:
PHP
问答标签:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
PHP安全开发:从白帽角度做安全 立即下载
PHP 2017.北京 全球开发者大会——高可用的PHP 立即下载
复杂PHP系统性能瓶颈排查及优化 立即下载