php通过正则提取页面指定内容实例

作者:袖梨 2022-06-25

例子代码如下,可常用于采集哦、

 代码如下 复制代码


1、获取页面标题

//提取标题
            preg_match('/(?<title>.*?)<\/title>/i', $html, $titleArr);<br />             $title = $titleArr['title'];<br /> 2、获取body主体内容,并将背景图片提取出来替换成其他图片地址</p> <p>/**<br />  * 获取BODY主体区域内容<br />  * @param $html<br />  * @param $urlRoot<br />  * @return mixed<br />  */<br /> function getBody($html,$urlRoot = null){<br />     //提取BODY主体<br />     preg_match('/<!--body-->(.*?)<!--body-->/is ', $html, $bodyArr);<br />     if(!$bodyArr){<br />         preg_match('/<body.*?>(.*?)<\/body>/is ', $html, $bodyArr);<br />     }<br />     $body = $bodyArr[1];<br />     //替换img文件<br />     $body =  preg_replace('/(<[img|IMG].*src=[\'|"])(\.\.\/)*(img.[^\'||^"]+)/',"$1$urlRoot$3",$body);<br />     //替换html文件内的css背景图片<br />     $body =  preg_replace('~\b(background(-image)?\s*:(.*?)\(\s*[\'|"]?)(\.\.\/)*(img.*?)?\s*\)~i',"$1$urlRoot$5)",$body);<br />     return $body;<br /> }<br /> 3、提取页面Description内容</p> <p>function getDescription($html){<br />     // Get the 'content' attribute value in a <meta name="description" ... /><br />     $matches = array();<br />  <br />     // Search for <meta name="description" content="Buy my stuff" /><br />     preg_match('/<meta.*?name=("|\')description("|\').*?content=("|\')(.*?)("|\')/i', $html, $matches);<br />     if (count($matches) > 4) {<br />         return trim($matches[4]);<br />     }<br />  <br />     // Order of attributes could be swapped around: <meta content="Buy my stuff" name="description" /><br />     preg_match('/<meta.*?content=("|\')(.*?)("|\').*?name=("|\')description("|\')/i', $html, $matches);<br />     if (count($matches) > 2) {<br />         return trim($matches[2]);<br />     }<br />  <br />     // No match<br />     return null;<br /> }<br /> 4、替换css文件的背景图片地址</p> <p>/**<br />  * 获取CSS内容<br />  * @param $cssCnt<br />  * @param $urlRoot<br />  * @return mixed<br />  */<br /> function getCss($cssCnt,$urlRoot =null){<br />     //匹配包含 img文件夹的相对路径图片 (含义绝对路径的不包含在其中)<br />     //匹配替换不一定准确,因为只是将 含义 ../ 的地址转为url 而没有考虑 ../../ 之类的层级关系<br />     $css =  preg_replace('~\b(background(-image)?\s*:(.*?)\(\s*[\'|"]?)(\.\.\/)*(img.*?)?\s*\)~i',"$1$urlRoot$5)",$cssCnt);<br />     //添加css前缀<br />     $css =  preg_replace('/\b.(.*?)[,|{]/',"pat .$0",$cssCnt);<br />     //TODO 压缩css<br />     return $css;<br /> }</p> <p><br />  </p> </td> </tr> </table> <p>从上面例子来看其实都是非常的简单就是批有规律的标签为开始与结束节点,这样我们可以获取这两个字符之类的内容也就是我们要提取的内容了哦,只是在中间有字符或空格的一些处理了哦。</p></td> </tr> </table> </div> </div> </section> <section class="wrap-box"> <div class="g-tit"> <h2>相关文章</h2> </div> <ul class="s-list nobord notop"> <li> <a href="/art-482104.htm" class="s-card"> <div class="s-card-l"> <p class="tit">dnf星辰百变获得方法大全 星辰百变获得方法是什么</p> <div class="info"> <span class="person">游戏攻略</span> <span class="time">2025-11-29</span> </div> </div> <div class="s-card-pic"> <img src="/images/lazy.gif" data-src="/uploads/20251128/logo_6928f4f9622361.jpg" alt="dnf星辰百变获得方法大全 星辰百变获得方法是什么" /> </div> </a> </li> <li> <a href="/art-482109.htm" class="s-card"> <div class="s-card-l"> <p class="tit">我本千金礼包兑换码2023 500钻永久(长期可用)激活码</p> <div class="info"> <span class="person">游戏攻略</span> <span class="time">2025-11-29</span> </div> </div> <div class="s-card-pic"> <img src="/images/lazy.gif" data-src="/uploads/20251128/logo_6928f4fbc44bd1.jpg" alt="我本千金礼包兑换码2023 500钻永久(长期可用)激活码" /> </div> </a> </li> <li> <a href="/art-482111.htm" class="s-card"> <div class="s-card-l"> <p class="tit">饥荒食人花种子 饥荒食人花种子怎么用与在哪里种植一览</p> <div class="info"> <span class="person">游戏攻略</span> <span class="time">2025-11-29</span> </div> </div> <div class="s-card-pic"> <img src="/images/lazy.gif" data-src="/uploads/20251128/logo_6928f4fc344581.png" alt="饥荒食人花种子 饥荒食人花种子怎么用与在哪里种植一览" /> </div> </a> </li> <li> <a href="/art-482112.htm" class="s-card"> <div class="s-card-l"> <p class="tit">dnf均衡仲裁者出什么 均衡仲裁者奖励汇总</p> <div class="info"> <span class="person">游戏攻略</span> <span class="time">2025-11-29</span> </div> </div> <div class="s-card-pic"> <img src="/images/lazy.gif" data-src="/uploads/20251128/logo_6928f4fccb64f1.jpg" alt="dnf均衡仲裁者出什么 均衡仲裁者奖励汇总" /> </div> </a> </li> <li> <a href="/art-482113.htm" class="s-card"> <div class="s-card-l"> <p class="tit">4月24号蚂蚁庄园小鸡课堂 小鸡庄园今日答案</p> <div class="info"> <span class="person">游戏攻略</span> <span class="time">2025-11-29</span> </div> </div> <div class="s-card-pic"> <img src="/images/lazy.gif" data-src="/uploads/20251128/logo_6928f4fce84391.jpg" alt="4月24号蚂蚁庄园小鸡课堂 小鸡庄园今日答案" /> </div> </a> </li> <li> <a href="/art-482114.htm" class="s-card"> <div class="s-card-l"> <p class="tit">dnf均衡仲裁者在哪进 均衡仲裁者深渊怎么开图</p> <div class="info"> <span class="person">游戏攻略</span> <span class="time">2025-11-29</span> </div> </div> <div class="s-card-pic"> <img src="/images/lazy.gif" data-src="/uploads/20251128/logo_6928f4fd6bdf91.jpg" alt="dnf均衡仲裁者在哪进 均衡仲裁者深渊怎么开图" /> </div> </a> </li> </ul> </section> <section class="wrap-box"> <div class="g-tit"> <h2>精彩推荐</h2> </div> <ul class="card-box"> <li class="card3"> <a href="/app/143931.htm" target="_self" class="figure"> <div class="figure-box"> <img src="/images/lazy.gif" data-src="https://img.111cn.net/uploads/20251128/logo_692956d82918c1.png" alt="白猫与龙王城" /> </div> <p class="figure-head">白猫与龙王城</p> <span class="figure-btn">下载</span> </a> </li> <li class="card3"> <a href="/app/143838.htm" target="_self" class="figure"> <div class="figure-box"> <img src="/images/lazy.gif" data-src="https://img.111cn.net/uploads/20251127/logo_6927e941371a31.png" alt="竞技场角斗士" /> </div> <p class="figure-head">竞技场角斗士</p> <span class="figure-btn">下载</span> </a> </li> <li class="card3"> <a href="/app/143754.htm" target="_self" class="figure"> <div class="figure-box"> <img src="/images/lazy.gif" data-src="https://img.111cn.net/uploads/20251126/logo_692697962336a1.png" alt="超凡火柴人" /> </div> <p class="figure-head">超凡火柴人</p> <span class="figure-btn">下载</span> </a> </li> <li class="card3"> <a href="/app/143711.htm" target="_self" class="figure"> <div class="figure-box"> <img src="/images/lazy.gif" data-src="https://img.111cn.net/uploads/20251126/logo_69264fc5ae4a31.png" alt="世界末日生存" /> </div> <p class="figure-head">世界末日生存</p> <span class="figure-btn">下载</span> </a> </li> </ul> <ul class="card-box-b"> <li class="card10"> <a href="/app/143757.htm" target="_self" class="figure2"> <div class="figure-box"> <img src="/images/lazy.gif" data-src="https://img.111cn.net/uploads/20251126/logo_6926979f4ac041.png" alt="托卡魔法乐园" /> </div> <div class="figure-cont"> <p class="figure-head">托卡魔法乐园</p> <div class="figure-desc"> <span>模拟经营</span> <span>托卡魔法乐园</span> </div> <div class="figure-desc"> <p>托卡魔法乐园模拟经营类游戏,以其精美治愈的画面与趣味生活元素</p> </div> </div> <span class="figure-btn">下载</span> </a> </li> <li class="card10"> <a href="/app/143421.htm" target="_self" class="figure2"> <div class="figure-box"> <img src="/images/lazy.gif" data-src="https://img.111cn.net/uploads/20251124/logo_692444ba9cd611.png" alt="商业传奇" /> </div> <div class="figure-cont"> <p class="figure-head">商业传奇</p> <div class="figure-desc"> <span>模拟经营</span> <span>商业传奇</span> </div> <div class="figure-desc"> <p>商业传奇是一款有意思的游戏。在游戏中玩家作为一个公司的老板,</p> </div> </div> <span class="figure-btn">下载</span> </a> </li> <li class="card10"> <a href="/app/143367.htm" target="_self" class="figure2"> <div class="figure-box"> <img src="/images/lazy.gif" data-src="https://img.111cn.net/uploads/20251120/logo_691e6753de1351.png" alt="权力与纷争华为版" /> </div> <div class="figure-cont"> <p class="figure-head">权力与纷争华为版</p> <div class="figure-desc"> <span>模拟经营</span> <span>权力与纷争华为版</span> </div> <div class="figure-desc"> <p>权力与纷争华为版,可用华为账号登录版本。您有看过哪个史诗级的</p> </div> </div> <span class="figure-btn">下载</span> </a> </li> <li class="card10"> <a href="/app/143197.htm" target="_self" class="figure2"> <div class="figure-box"> <img src="/images/lazy.gif" data-src="https://img.111cn.net/uploads/20251118/logo_691bc7e1b6fe01.png" alt="北欧传奇1" /> </div> <div class="figure-cont"> <p class="figure-head">北欧传奇1</p> <div class="figure-desc"> <span>模拟经营</span> <span>北欧传奇1</span> </div> <div class="figure-desc"> <p>北欧传奇1是采用精致卡通风格的模拟经营游戏,您将扮演拉格纳国</p> </div> </div> <span class="figure-btn">下载</span> </a> </li> <li class="card10"> <a href="/app/143008.htm" target="_self" class="figure2"> <div class="figure-box"> <img src="/images/lazy.gif" data-src="https://img.111cn.net/uploads/20251117/logo_691a73e9d913a1.png" alt="平民崛起记" /> </div> <div class="figure-cont"> <p class="figure-head">平民崛起记</p> <div class="figure-desc"> <span>模拟经营</span> <span>平民崛起记</span> </div> <div class="figure-desc"> <p>平民崛起记带来有趣的模拟经营体验,游戏采用卡通风格画面设计。</p> </div> </div> <span class="figure-btn">下载</span> </a> </li> </ul> </section> <footer class="foot"> <a href="/" class="logo-icon"> <img src="/mobile/images/logo2.png" alt="一聚教程网"> </a> <p>Copyright © 2010-2022</p> <p>111cn.net All Rights Reserved</p> </footer> <script> var advData = {"img_fixed_pc_adv":"https:\/\/img.111cn.net\/uploads\/20240509\/663c2e9729f58.jpg","img_fixed_mob_adv":"https:\/\/img.111cn.net\/uploads\/20240509\/663c2e8793225.jpg","url_adv":"http:\/\/shop.hushen.cn\/shop\/c\/baojianpin.html","str_adv":"\u864e\u795e\u5546\u57ce\uff1a\u5173\u7231\u7537\u6027\uff0c\u66f4\u61c2\u7537\u4eba\u3002\u89e3\u51b3\u5927\u4f17\u7684\u7537\u8a00\u4e4b\u9690","img_popup_adv":"https:\/\/img.111cn.net\/uploads\/20240509\/663c2e748238d.png","pc_show_img":"2","pc_show_popup":"2","pc_show_video":"2","mob_show_img":"2","mob_show_popup":"2","mob_show_video":"2","close_adv":"https:\/\/img.111cn.net\/uploads\/20240508\/663b20650801e.png","video_adv":"\/pc\/images\/pc-adv.mp4"}; </script> <script src="/jspc/funcmob.js" type="text/javascript"></script> <!-- Google tag (gtag.js) --> <script async src="https://www.googletagmanager.com/gtag/js?id=G-DSRRGRV1TL"></script> <script> window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); gtag('config', 'G-DSRRGRV1TL'); </script> <div class="back-top" style="display: block;"> <span class="icon-box"> <svg class="icon" viewBox="0 0 1024 1024"> <path d="M213.333333 640h170.666667v256h256v-256h170.666667l-298.666667-341.333333zM170.666667 128h682.666666v85.333333H170.666667z" fill="#0374f3"></path> </svg> </span> </div> </div> <script src="/js/stat.js"></script> </body> </html>