首页 > php获取某个class类的img数量并输出

php获取某个class类的img数量并输出

<div class="post-content">
<img src……
<img src……
<img src……
</div>

php怎么统计 post-content 内的img数量,并且输出数量,如上面代码,img数量是3


preg_match_all正则匹配行么?

$text = '<div class="post-content"><img src="" /><img src="" /></div>';
$preg = "/(\<img.*?\/\>)/s";
echo preg_match_all($preg, $text, $match);

为什么不用jQuery呢
$('.post-content img').length();


用正则来解析 html 是不推荐的做法,见 http://stackoverflow.com/questions/17... ,最好使用一些 PHP 的 dom 解析库。

  1. http://simplehtmldom.sourceforge.net/
  2. http://stackoverflow.com/questions/29...
  3. http://stackoverflow.com/questions/35...
【热门文章】
【热门文章】