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-422670.htm" class="s-card"> <div class="s-card-l"> <p class="tit">无期迷途派因怎么样</p> <div class="info"> <span class="person">游戏攻略</span> <span class="time">2024-11-27</span> </div> </div> <div class="s-card-pic"> <img src="/images/lazy.gif" data-src="/uploads/20241127/logo_6746b563b462c1.jpeg" alt="无期迷途派因怎么样" /> </div> </a> </li> <li> <a href="/art-422669.htm" class="s-card"> <div class="s-card-l"> <p class="tit">阴阳师季四时承青怎么样 阴阳师季四时承青皮肤介绍</p> <div class="info"> <span class="person">游戏攻略</span> <span class="time">2024-11-27</span> </div> </div> <div class="s-card-pic"> <img src="/images/lazy.gif" data-src="/uploads/20241127/logo_6746aeeaa9ea01.jpeg" alt="阴阳师季四时承青怎么样 阴阳师季四时承青皮肤介绍" /> </div> </a> </li> <li> <a href="/art-422668.htm" class="s-card"> <div class="s-card-l"> <p class="tit">阴阳师神酿星熊童子怎么样 阴阳师神酿星熊童子式神介绍</p> <div class="info"> <span class="person">游戏攻略</span> <span class="time">2024-11-27</span> </div> </div> <div class="s-card-pic"> <img src="/images/lazy.gif" data-src="/uploads/20241127/logo_6746aee7f31481.jpeg" alt="阴阳师神酿星熊童子怎么样 阴阳师神酿星熊童子式神介绍" /> </div> </a> </li> <li> <a href="/art-422667.htm" class="s-card"> <div class="s-card-l"> <p class="tit">炉石传说融合进化猎卡组怎么样</p> <div class="info"> <span class="person">游戏攻略</span> <span class="time">2024-11-27</span> </div> </div> <div class="s-card-pic"> <img src="/images/lazy.gif" data-src="/uploads/20241127/logo_6746a7527883b1.jpeg" alt="炉石传说融合进化猎卡组怎么样" /> </div> </a> </li> <li> <a href="/art-422666.htm" class="s-card"> <div class="s-card-l"> <p class="tit">崩坏星穹铁道星期日遗器怎么选择</p> <div class="info"> <span class="person">游戏攻略</span> <span class="time">2024-11-27</span> </div> </div> <div class="s-card-pic"> <img src="/images/lazy.gif" data-src="/uploads/20241127/logo_67469944611af1.jpeg" alt="崩坏星穹铁道星期日遗器怎么选择" /> </div> </a> </li> <li> <a href="/art-422665.htm" class="s-card"> <div class="s-card-l"> <p class="tit">群英风华录兑换码分享</p> <div class="info"> <span class="person">游戏攻略</span> <span class="time">2024-11-27</span> </div> </div> <div class="s-card-pic"> <img src="/images/lazy.gif" data-src="/uploads/20241127/logo_67469943819571.jpeg" alt="群英风华录兑换码分享" /> </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/99428.htm" target="_self" class="figure"> <div class="figure-box"> <img src="/images/lazy.gif" data-src="https://img.111cn.net/uploads/20241018/671258fc7c56f.png" alt="沙威玛传奇安卓版" /> </div> <p class="figure-head">沙威玛传奇安卓版</p> <span class="figure-btn">下载</span> </a> </li> <li class="card3"> <a href="/app/99427.htm" target="_self" class="figure"> <div class="figure-box"> <img src="/images/lazy.gif" data-src="https://img.111cn.net/uploads/20241018/6711fc29100e3.png" alt="战盟大乱斗最新版" /> </div> <p class="figure-head">战盟大乱斗最新版</p> <span class="figure-btn">下载</span> </a> </li> <li class="card3"> <a href="/app/99426.htm" target="_self" class="figure"> <div class="figure-box"> <img src="/images/lazy.gif" data-src="https://img.111cn.net/uploads/20241018/6711ef93e4d90.png" alt="大侠立志传最新版" /> </div> <p class="figure-head">大侠立志传最新版</p> <span class="figure-btn">下载</span> </a> </li> <li class="card3"> <a href="/app/99425.htm" target="_self" class="figure"> <div class="figure-box"> <img src="/images/lazy.gif" data-src="https://img.111cn.net/uploads/20241018/6711ca5ba8572.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/99454.htm" target="_self" class="figure2"> <div class="figure-box"> <img src="/images/lazy.gif" data-src="https://img.111cn.net/uploads/20241019/671393f16002e.jpg" 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/99397.htm" target="_self" class="figure2"> <div class="figure-box"> <img src="/images/lazy.gif" data-src="https://img.111cn.net/uploads/20241015/670dc626d93dd.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/99010.htm" target="_self" class="figure2"> <div class="figure-box"> <img src="/images/lazy.gif" data-src="https://img.111cn.net/uploads/20240907/logo_66dc0ba5ab7021.jpg" 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/99001.htm" target="_self" class="figure2"> <div class="figure-box"> <img src="/images/lazy.gif" data-src="https://img.111cn.net/uploads/20240907/logo_66dc094ae30941.jpg" 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/98996.htm" target="_self" class="figure2"> <div class="figure-box"> <img src="/images/lazy.gif" data-src="https://img.111cn.net/uploads/20240907/logo_66dbd35fadafd1.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>