<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 解析库。
- http://simplehtmldom.sourceforge.net/
- http://stackoverflow.com/questions/29...
- http://stackoverflow.com/questions/35...