


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 tag, or all the content in the 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);
Look at the result directly:
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
