PHP截取网页源码内所有<a>标签并形成数组的方法
如果我们需要获取一个网页的源码,并从中获取一些自己需要的信息,比如标签,或者标签当中的所有内容和的时候。我们就必须要截取,然而php截取函数,相当的操蛋,原谅博主是个新手,至少我找了很久,并没有发现有向左截取字符串的函数,像别人的答案substr()配合获取字符串位置的函数,然后截取。我只能说试过,并不好用。然后还有些七七八八的办法,统统不实用,麻烦。接下来我就要说一种方法了,相当的BUG,相当的好用。
$url = "http://www.texnet.com.cn/product"; $result = file_get_contents($url); $str = intercept_str($result, '粘上你要开始截取的字符串比如:<p class="productsearch">', '粘上结束时刻的字符串比如:<p class="greenblock1">', 1); //需要先去除所有的换行符、制表符、回车等等,html源文件由于上述符号的存在会造成无法匹配。 $hre = preg_replace("/[\t\n\r]+/","",$str); $reg='/<a .*?>.*?<\/a>/';//这个正则可以自己发挥,博主只是随便放了个匹配<a>标签的 preg_match_all($reg,$hre,$aarray);
//然后就可以打印了呀
dump($aarray);
直接看结果:
array(1) { [0] => array(96) { [0] => string(195) "<a href="http://jinxiang.cn.texnet.com.cn" title="桐乡市锦翔纺织有限公司" target="_blank" class="L14 bigblue2"> <font color='#FF0000'>桐乡市</font>锦翔纺织有限公司 </a>" [1] => string(115) "<a href="http://jinxiang.cn.texnet.com.cn/pdetail--840799.html" title="绢丝 " target="_blank" > 绢丝 </a>" [2] => string(113) "<a href="http://jinxiang.cn.texnet.com.cn/pdetail--840798.html" title="绢丝" target="_blank" > 绢丝 </a>" [3] => string(131) "<a href="http://jinxiang.cn.texnet.com.cn/pdetail--840802.html" title="羊毛混纺纱" target="_blank" > 羊毛混纺纱 </a>" [4] => string(131) "<a href="http://jinxiang.cn.texnet.com.cn/pdetail--840800.html" title="绢丝混纺纱" target="_blank" > 绢丝混纺纱 </a>" [5] => string(119) "<a href="http://jinxiang.cn.texnet.com.cn/pdetail--840801.html" title="毛腈纱" target="_blank" > 毛腈纱 </a>" [6] => string(122) "<a href="http://jinxiang.cn.texnet.com.cn" target="_blank" class="green L130 english">http://jinxiang.cn.texnet.com.cn</a>" [7] => string(130) "<a target="_blank" href="/enquiry/?t=company&id=2056"><img src="/images/enquire.gif" style="cursor:pointor" align="absbottom"></a>" [8] => string(205) "<a href="http://zjhtfs.cn.texnet.com.cn" title="桐乡市海通毛绒服饰有限公司" target="_blank" class="L14 bigblue2"> <font color='#FF0000'>桐乡市</font>海通毛绒服饰有限公司 </a>" [9] => string(111) "<a href="http://zjhtfs.cn.texnet.com.cn/pdetail--939047.html" title="粗纱" target="_blank" > 粗纱 </a>" [10] => string(117) "<a href="http://zjhtfs.cn.texnet.com.cn/pdetail--939048.html" title="粗纺纱" target="_blank" > 粗纺纱 </a>" [11] => string(135) "<a href="http://zjhtfs.cn.texnet.com.cn/pdetail--939033.html" title="粗纺羊仔纱线" target="_blank" > 粗纺羊仔纱线 </a>" [12] => string(117) "<a href="http://zjhtfs.cn.texnet.com.cn/pdetail--939043.html" title="丝羊绒" target="_blank" > 丝羊绒 </a>" [13] => string(117) "<a href="http://zjhtfs.cn.texnet.com.cn/pdetail--939046.html" title="羊绒纱" target="_blank" > 羊绒纱 </a>" [14] => string(118) "<a href="http://zjhtfs.cn.texnet.com.cn" target="_blank" class="green L130 english">http://zjhtfs.cn.texnet.com.cn</a>" [15] => string(129) "<a target="_blank" href="/enquiry/?t=company&id=768"><img src="/images/enquire.gif" style="cursor:pointor" align="absbottom"></a>" [16] => string(193) "<a href="http://xtknit.cn.texnet.com.cn" title="桐乡市新拓针织有限公司" target="_blank" class="L14 bigblue2"> <font color='#FF0000'>桐乡市</font>新拓针织有限公司 </a>" [17] => string(117) "<a href="http://xtknit.cn.texnet.com.cn/pdetail--750913.html" title="卫衣布" target="_blank" > 卫衣布 </a>" [18] => string(117) "<a href="http://xtknit.cn.texnet.com.cn/pdetail--750931.html" title="烂花布" target="_blank" > 烂花布 </a>" [19] => string(117) "<a href="http://xtknit.cn.texnet.com.cn/pdetail--750927.html" title="烫金布" target="_blank" > 烫金布 </a>" [20] => string(123) "<a href="http://xtknit.cn.texnet.com.cn/pdetail--750907.html" title="彩条汗布" target="_blank" > 彩条汗布 </a>" [21] => string(111) "<a href="http://xtknit.cn.texnet.com.cn/pdetail--750908.html" title="汗布" target="_blank" > 汗布 </a>" [22] => string(118) "<a href="http://xtknit.cn.texnet.com.cn" target="_blank" class="green L130 english">http://xtknit.cn.texnet.com.cn</a>" [23] => string(129) "<a target="_blank" href="/enquiry/?t=company&id=828"><img src="/images/enquire.gif" style="cursor:pointor" align="absbottom"></a>" [24] => string(193) "<a href="http://zjyuxi.cn.texnet.com.cn" title="桐乡市玉溪针织有限公司" target="_blank" class="L14 bigblue2"> <font color='#FF0000'>桐乡市</font>玉溪针织有限公司 </a>" [25] => string(117) "<a href="http://zjyuxi.cn.texnet.com.cn/pdetail--691497.html" title="印花布" target="_blank" > 印花布 </a>" [26] => string(135) "<a href="http://zjyuxi.cn.texnet.com.cn/pdetail--691378.html" title="人棉氨纶汗布" target="_blank" > 人棉氨纶汗布 </a>" [27] => string(111) "<a href="http://zjyuxi.cn.texnet.com.cn/pdetail--691383.html" title="汗布" target="_blank" > 汗布 </a>" [28] => string(141) "<a href="http://zjyuxi.cn.texnet.com.cn/pdetail--691281.html" title="人棉氨纶印花布" target="_blank" > 人棉氨纶印花布 </a>" [29] => string(116) "<a href="http://zjyuxi.cn.texnet.com.cn/pdetail--10418.html" title="罗马布" target="_blank" > 罗马布 </a>" [30] => string(118) "<a href="http://zjyuxi.cn.texnet.com.cn" target="_blank" class="green L130 english">http://zjyuxi.cn.texnet.com.cn</a>" [31] => string(130) "<a target="_blank" href="/enquiry/?t=company&id=1589"><img src="/images/enquire.gif" style="cursor:pointor" align="absbottom"></a>" [32] => string(192) "<a href="http://zhonglisilk.cn.texnet.com.cn" title="桐乡市洲泉众力丝织厂" target="_blank" class="L14 bigblue2"> <font color='#FF0000'>桐乡市</font>洲泉众力丝织厂 </a>" [33] => string(129) "<a href="http://zhonglisilk.cn.texnet.com.cn/pdetail--1013163.html" title="亚麻彩条" target="_blank" > 亚麻彩条 </a>" [34] => string(129) "<a href="http://zhonglisilk.cn.texnet.com.cn/pdetail--1013180.html" title="亚麻素色" target="_blank" > 亚麻素色 </a>" [35] => string(147) "<a href="http://zhonglisilk.cn.texnet.com.cn/pdetail--1013190.html" title="亚麻烫金针织布" target="_blank" > 亚麻烫金针织布 </a>" [36] => string(129) "<a href="http://zhonglisilk.cn.texnet.com.cn/pdetail--1013192.html" title="亚麻印花" target="_blank" > 亚麻印花 </a>" [37] => string(141) "<a href="http://zhonglisilk.cn.texnet.com.cn/pdetail--1013453.html" title="人棉竹节汗布" target="_blank" > 人棉竹节汗布 </a>" [38] => string(128) "<a href="http://zhonglisilk.cn.texnet.com.cn" target="_blank" class="green L130 english">http://zhonglisilk.cn.texnet.com.cn</a>" [39] => string(130) "<a target="_blank" href="/enquiry/?t=company&id=1918"><img src="/images/enquire.gif" style="cursor:pointor" align="absbottom"></a>" [40] => string(207) "<a href="http://zjqinghe.cn.texnet.com.cn" title="桐乡市清瀛家居用品有限公司" target="_blank" class="L14 bigblue2"> <font color='#FF0000'>桐乡市</font>清瀛家居用品有限公司 </a>" [41] => string(112) "<a href="http://zjqinghe.cn.texnet.com.cn/pdetail--45530.html" title="靠垫" target="_blank" > 靠垫 </a>" [42] => string(118) "<a href="http://zjqinghe.cn.texnet.com.cn/pdetail--50861.html" title="三件套" target="_blank" > 三件套 </a>" [43] => string(113) "<a href="http://zjqinghe.cn.texnet.com.cn/pdetail--712086.html" title="被子" target="_blank" > 被子 </a>" [44] => string(113) "<a href="http://zjqinghe.cn.texnet.com.cn/pdetail--712130.html" title="毯子" target="_blank" > 毯子 </a>" [45] => string(113) "<a href="http://zjqinghe.cn.texnet.com.cn/pdetail--712159.html" title="垫子" target="_blank" > 垫子 </a>" [46] => string(122) "<a href="http://zjqinghe.cn.texnet.com.cn" target="_blank" class="green L130 english">http://zjqinghe.cn.texnet.com.cn</a>" [47] => string(130) "<a target="_blank" href="/enquiry/?t=company&id=1930"><img src="/images/enquire.gif" style="cursor:pointor" align="absbottom"></a>" [48] => string(203) "<a href="http://hlzr.cn.texnet.com.cn" title="桐乡市华灵丝绸有限责任公司" target="_blank" class="L14 bigblue2"> <font color='#FF0000'>桐乡市</font>华灵丝绸有限责任公司 </a>" [49] => string(110) "<a href="http://hlzr.cn.texnet.com.cn/pdetail--1121956.html" title="印花" target="_blank" > 印花 </a>" [50] => string(122) "<a href="http://hlzr.cn.texnet.com.cn/pdetail--1121947.html" title="长毛植绒" target="_blank" > 长毛植绒 </a>" [51] => string(122) "<a href="http://hlzr.cn.texnet.com.cn/pdetail--1121940.html" title="鞋材面料" target="_blank" > 鞋材面料 </a>" [52] => string(122) "<a href="http://hlzr.cn.texnet.com.cn/pdetail--1121933.html" title="服装面料" target="_blank" > 服装面料 </a>" [53] => string(116) "<a href="http://hlzr.cn.texnet.com.cn/pdetail--1121884.html" title="仿羊绒" target="_blank" > 仿羊绒 </a>" [54] => string(114) "<a href="http://hlzr.cn.texnet.com.cn" target="_blank" class="green L130 english">http://hlzr.cn.texnet.com.cn</a>" [55] => string(130) "<a target="_blank" href="/enquiry/?t=company&id=2190"><img src="/images/enquire.gif" style="cursor:pointor" align="absbottom"></a>" [56] => string(198) "<a href="http://chenhuisilk.cn.texnet.com.cn" title="桐乡市晨晖丝绸有限公司" target="_blank" class="L14 bigblue2"> <font color='#FF0000'>桐乡市</font>晨晖丝绸有限公司 </a>" [57] => string(135) "<a href="http://chenhuisilk.cn.texnet.com.cn/pdetail--1521909.html" title="大提花面料" target="_blank" > 大提花面料 </a>" [58] => string(135) "<a href="http://chenhuisilk.cn.texnet.com.cn/pdetail--1522013.html" title="人丝美丽绸" target="_blank" > 人丝美丽绸 </a>" [59] => string(135) "<a href="http://chenhuisilk.cn.texnet.com.cn/pdetail--1522029.html" title="涤丝斜纹绸" target="_blank" > 涤丝斜纹绸 </a>" [60] => string(129) "<a href="http://chenhuisilk.cn.texnet.com.cn/pdetail--1522061.html" title="装饰里布" target="_blank" > 装饰里布 </a>" [61] => string(122) "<a href="http://chenhuisilk.cn.texnet.com.cn/pdetail--612727.html" title="小提花" target="_blank" > 小提花 </a>" [62] => string(128) "<a href="http://chenhuisilk.cn.texnet.com.cn" target="_blank" class="green L130 english">http://chenhuisilk.cn.texnet.com.cn</a>" [63] => string(130) "<a target="_blank" href="/enquiry/?t=company&id=2434"><img src="/images/enquire.gif" style="cursor:pointor" align="absbottom"></a>" [64] => string(193) "<a href="http://jhknit.cn.texnet.com.cn" title="桐乡市嘉鸿针织有限公司" target="_blank" class="L14 bigblue2"> <font color='#FF0000'>桐乡市</font>嘉鸿针织有限公司 </a>" [65] => string(152) "<a href="http://jhknit.cn.texnet.com.cn/pdetail--41316.html" title="彩条丝光棉珠地网眼" target="_blank" > 彩条丝光棉珠地网眼 </a>" [66] => string(140) "<a href="http://jhknit.cn.texnet.com.cn/pdetail--41315.html" title="烫银纯亚麻汗布" target="_blank" > 烫银纯亚麻汗布 </a>" [67] => string(146) "<a href="http://jhknit.cn.texnet.com.cn/pdetail--41313.html" title="双丝光棉彩条罗纹" target="_blank" > 双丝光棉彩条罗纹 </a>" [68] => string(134) "<a href="http://jhknit.cn.texnet.com.cn/pdetail--41322.html" title="彩条氨纶汗布" target="_blank" > 彩条氨纶汗布 </a>" [69] => string(116) "<a href="http://jhknit.cn.texnet.com.cn/pdetail--41310.html" title="罗马布" target="_blank" > 罗马布 </a>" [70] => string(118) "<a href="http://jhknit.cn.texnet.com.cn" target="_blank" class="green L130 english">http://jhknit.cn.texnet.com.cn</a>" [71] => string(130) "<a target="_blank" href="/enquiry/?t=company&id=8694"><img src="/images/enquire.gif" style="cursor:pointor" align="absbottom"></a>" [72] => string(198) "<a href="http://kangtaisilk.cn.texnet.com.cn" title="桐乡市康泰织造有限公司" target="_blank" class="L14 bigblue2"> <font color='#FF0000'>桐乡市</font>康泰织造有限公司 </a>" [73] => string(122) "<a href="http://kangtaisilk.cn.texnet.com.cn/pdetail--716322.html" title="丝绵绸" target="_blank" > 丝绵绸 </a>" [74] => string(134) "<a href="http://kangtaisilk.cn.texnet.com.cn/pdetail--716326.html" title="色织双宫绸" target="_blank" > 色织双宫绸 </a>" [75] => string(116) "<a href="http://kangtaisilk.cn.texnet.com.cn/pdetail--716324.html" title="真丝" target="_blank" > 真丝 </a>" [76] => string(134) "<a href="http://kangtaisilk.cn.texnet.com.cn/pdetail--716321.html" title="涤丝双宫绸" target="_blank" > 涤丝双宫绸 </a>" [77] => string(134) "<a href="http://kangtaisilk.cn.texnet.com.cn/pdetail--716703.html" title="缎染双宫绸" target="_blank" > 缎染双宫绸 </a>" [78] => string(128) "<a href="http://kangtaisilk.cn.texnet.com.cn" target="_blank" class="green L130 english">http://kangtaisilk.cn.texnet.com.cn</a>" [79] => string(131) "<a target="_blank" href="/enquiry/?t=company&id=15390"><img src="/images/enquire.gif" style="cursor:pointor" align="absbottom"></a>" [80] => string(205) "<a href="http://bysilk.cn.texnet.com.cn" title="桐乡市博阳丝绸织造有限公司" target="_blank" class="L14 bigblue2"> <font color='#FF0000'>桐乡市</font>博阳丝绸织造有限公司 </a>" [81] => string(111) "<a href="http://bysilk.cn.texnet.com.cn/pdetail--844020.html" title="丝麻" target="_blank" > 丝麻 </a>" [82] => string(111) "<a href="http://bysilk.cn.texnet.com.cn/pdetail--843991.html" title="围巾" target="_blank" > 围巾 </a>" [83] => string(123) "<a href="http://bysilk.cn.texnet.com.cn/pdetail--843992.html" title="羊绒围巾" target="_blank" > 羊绒围巾 </a>" [84] => string(129) "<a href="http://bysilk.cn.texnet.com.cn/pdetail--843993.html" title="丝羊绒围巾" target="_blank" > 丝羊绒围巾 </a>" [85] => string(123) "<a href="http://bysilk.cn.texnet.com.cn/pdetail--843994.html" title="真丝围巾" target="_blank" > 真丝围巾 </a>" [86] => string(118) "<a href="http://bysilk.cn.texnet.com.cn" target="_blank" class="green L130 english">http://bysilk.cn.texnet.com.cn</a>" [87] => string(132) "<a target="_blank" href="/enquiry/?t=company&id=194739"><img src="/images/enquire.gif" style="cursor:pointor" align="absbottom"></a>" [88] => string(203) "<a href="http://huafangtex.cn.texnet.com.cn" title="桐乡市华方纺织品有限公司" target="_blank" class="L14 bigblue2"> <font color='#FF0000'>桐乡市</font>华方纺织品有限公司 </a>" [89] => string(122) "<a href="http://huafangtex.cn.texnet.com.cn/pdetail--1349605.html" title="毛圈布" target="_blank" > 毛圈布 </a>" [90] => string(122) "<a href="http://huafangtex.cn.texnet.com.cn/pdetail--1349676.html" title="双面布" target="_blank" > 双面布 </a>" [91] => string(116) "<a href="http://huafangtex.cn.texnet.com.cn/pdetail--1349970.html" title="汗布" target="_blank" > 汗布 </a>" [92] => string(128) "<a href="http://huafangtex.cn.texnet.com.cn/pdetail--1350037.html" title="人棉汗布" target="_blank" > 人棉汗布 </a>" [93] => string(140) "<a href="http://huafangtex.cn.texnet.com.cn/pdetail--1349631.html" title="全棉竹节毛圈" target="_blank" > 全棉竹节毛圈 </a>" [94] => string(126) "<a href="http://huafangtex.cn.texnet.com.cn" target="_blank" class="green L130 english">http://huafangtex.cn.texnet.com.cn</a>" [95] => string(132) "<a target="_blank" href="/enquiry/?t=company&id=236133"><img src="/images/enquire.gif" style="cursor:pointor" align="absbottom"></a>" } }
是不是很简单,很暴力,很直接。
以上是PHP截取网页源码内所有&lt;a&gt;标签并形成数组的方法的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

热门话题

PHP 8.4 带来了多项新功能、安全性改进和性能改进,同时弃用和删除了大量功能。 本指南介绍了如何在 Ubuntu、Debian 或其衍生版本上安装 PHP 8.4 或升级到 PHP 8.4

Visual Studio Code,也称为 VS Code,是一个免费的源代码编辑器 - 或集成开发环境 (IDE) - 可用于所有主要操作系统。 VS Code 拥有针对多种编程语言的大量扩展,可以轻松编写

如果您是一位经验丰富的 PHP 开发人员,您可能会感觉您已经在那里并且已经完成了。您已经开发了大量的应用程序,调试了数百万行代码,并调整了一堆脚本来实现操作

本教程演示了如何使用PHP有效地处理XML文档。 XML(可扩展的标记语言)是一种用于人类可读性和机器解析的多功能文本标记语言。它通常用于数据存储

JWT是一种基于JSON的开放标准,用于在各方之间安全地传输信息,主要用于身份验证和信息交换。1.JWT由Header、Payload和Signature三部分组成。2.JWT的工作原理包括生成JWT、验证JWT和解析Payload三个步骤。3.在PHP中使用JWT进行身份验证时,可以生成和验证JWT,并在高级用法中包含用户角色和权限信息。4.常见错误包括签名验证失败、令牌过期和Payload过大,调试技巧包括使用调试工具和日志记录。5.性能优化和最佳实践包括使用合适的签名算法、合理设置有效期、

字符串是由字符组成的序列,包括字母、数字和符号。本教程将学习如何使用不同的方法在PHP中计算给定字符串中元音的数量。英语中的元音是a、e、i、o、u,它们可以是大写或小写。 什么是元音? 元音是代表特定语音的字母字符。英语中共有五个元音,包括大写和小写: a, e, i, o, u 示例 1 输入:字符串 = "Tutorialspoint" 输出:6 解释 字符串 "Tutorialspoint" 中的元音是 u、o、i、a、o、i。总共有 6 个元

静态绑定(static::)在PHP中实现晚期静态绑定(LSB),允许在静态上下文中引用调用类而非定义类。1)解析过程在运行时进行,2)在继承关系中向上查找调用类,3)可能带来性能开销。

PHP的魔法方法有哪些?PHP的魔法方法包括:1.\_\_construct,用于初始化对象;2.\_\_destruct,用于清理资源;3.\_\_call,处理不存在的方法调用;4.\_\_get,实现动态属性访问;5.\_\_set,实现动态属性设置。这些方法在特定情况下自动调用,提升代码的灵活性和效率。
