Home >Backend Development >PHP Tutorial >PHP method to intercept all <a> tags in the web page source code and form an array
If we need to obtain the source code of a web page and get some information we need from it, such as the 3499910bf9dac5ae3c52d5ede7383485 tag, or all the content in the e388a4556c0f65e1904146cc1a846beeff6d136ddc5fdfeffaf53ff6ee95f185 tag. We have to intercept, but the PHP interception function is quite fucked. Forgive the blogger for being a novice. At least I searched for a long time and did not find a function to intercept the string to the left. Like other people's answers, substr() is used to obtain the string. The position function is then intercepted. All I can say is that I tried it and it didn’t work. Then there are some various methods, all of which are impractical and troublesome. Next I will talk about a method, which is quite buggy and quite easy to use.
$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>" } }
Isn’t it very simple, very violent, very direct.
The above is the detailed content of PHP method to intercept all <a> tags in the web page source code and form an array. For more information, please follow other related articles on the PHP Chinese website!