Home > Backend Development > PHP Tutorial > php获取某个class类的img数量并输出

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

WBOY
Release: 2016-06-06 20:50:53
Original
1236 people have browsed it

<div class="post-content">
<img  src alt="php获取某个class类的img数量并输出" ><p>php怎么统计 post-content 内的img数量,并且输出数量,如上面代码,img数量是3</p>
                            
                        


                                                                                                                        
                     <h2>回复内容:</h2>
                      
                                                            <pre class="brush:php;toolbar:false"><div class="post-content">
<img  src alt="php获取某个class类的img数量并输出" ><p>php怎么统计 post-content 内的img数量,并且输出数量,如上面代码,img数量是3</p>
                            
                        
            <p class="answer fmt" data-id="1020000000166314">
                                    </p>
<p>用<code>preg_match_all</code>正则匹配行么?</p>
<pre class="brush:php;toolbar:false">$text = '<div class="post-content">
<img  src="" alt="php获取某个class类的img数量并输出" ><img  src="" alt="php获取某个class类的img数量并输出" >
</div>';
$preg = "/(\<img . alt="php获取某个class类的img数量并输出" >)/s";
echo preg_match_all($preg, $text, $match);</img.>
Copy after login

用正则来解析 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...

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

Related labels:
php
source:php.cn
Previous article:linux - PHP页面有一个浏览量统计每秒有200并发,造成insert给mysql造成很大压力 Next article:Mysql如何准确赛选(排序)出最值得推荐的信息?
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Latest Issues
Related Topics
More>
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template