php获取某个class类的img数量并输出
PHPz
PHPz 2017-04-10 14:24:40
0
3
558
<p class="post-content">
<img src……
<img src……
<img src……
</p>

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

PHPz
PHPz

学习是最好的投资!

全員に返信(3)
Peter_Zhu

preg_match_all正则匹配行么?

$text = '<p class="post-content"><img src="" /><img src="" /></p>';
$preg = "/(\<img.*?\/\>)/s";
echo preg_match_all($preg, $text, $match);
いいねを押す +0
伊谢尔伦

用正则来解析 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...
いいねを押す +0
大家讲道理

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

いいねを押す +0
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート