PHP截取网页源码内所有<a>标签并形成数组的方法

浅宇小哥_7360

浅宇小哥_7360

2018-03-16

2529人浏览

原创

如果我们需要获取一个网页的源码,并从中获取一些自己需要的信息,比如标签,或者

    标签当中的所有内容和的时候。我们就必须要截取,然而php截取函数,相当的操蛋,原谅博主是个新手,至少我找了很久,并没有发现有向左截取字符串的函数,像别人的答案substr()配合获取字符串位置的函数,然后截取。我只能说试过,并不好用。然后还有些七七八八的办法,统统不实用,麻烦。接下来我就要说一种方法了,相当的bug,相当的好用。

$url = "http://www.texnet.com.cn/product";
$result = file_get_contents($url);
$str = intercept_str($result, '粘上你要开始截取的字符串比如:<p>', '粘上结束时刻的字符串比如:</p><div class="aritcle_card flexRow artxards">
											<div class="artcardd flexRow">
												<a class="aritcle_card_img" rel="nofollow" href="/xiazai/skill13222" title="Build MCP servers and clients for PHP applications with the official PHP SDK"><img
														src="https://img.php.cn/upload/skill/001/246/273/6adaf20e352aa140.jpg" alt="Build MCP servers and clients for PHP applications with the official PHP SDK" onerror="this.onerror='';this.src='/static/lhimages/moren/morentu.png'" ></a>
												<div class="aritcle_card_info flexColumn">
													<a rel="nofollow" href="/xiazai/skill13222" title="Build MCP servers and clients for PHP applications with the official PHP SDK" class="overflowclass">Build MCP servers and clients for PHP applications with the official PHP SDK</a>
													<p class="overflowclass">使用官方Model Context Protocol PHP SDK将PHP应用程序功能暴露为MCP工具、资源和提示,用于代理工作流。</p>
												</div>
												<a rel="nofollow" href="/xiazai/skill13222" title="Build MCP servers and clients for PHP applications with the official PHP SDK" class="aritcle_card_btn flexRow flexcenter"><b></b><span>下载</span>
												</a>
											</div>
										</div><p>', 1);
//需要先去除所有的换行符、制表符、回车等等,html源文件由于上述符号的存在会造成无法匹配。
$hre = preg_replace("/[\t\n\r]+/","",$str);
$reg='/<a>.*?/';//这个正则可以自己发挥,博主只是随便放了个匹配</a><a>标签的
preg_match_all($reg,$hre,$aarray);</a></p>
//然后就可以打印了呀
dump($aarray);

直接看结果:

array(1) {
  [0] => array(96) {
    [0] => string(195) "<a>     <font>桐乡市</font>锦翔纺织有限公司  </a>"
    [1] => string(115) "<a>     绢丝   </a>"
    [2] => string(113) "<a>     绢丝  </a>"
    [3] => string(131) "<a>     羊毛混纺纱  </a>"
    [4] => string(131) "<a>     绢丝混纺纱  </a>"
    [5] => string(119) "<a>     毛腈纱  </a>"
    [6] => string(122) "<a>http://jinxiang.cn.texnet.com.cn</a>"
    [7] => string(130) "<a>@@##@@</a>"
    [8] => string(205) "<a>     <font>桐乡市</font>海通毛绒服饰有限公司  </a>"
    [9] => string(111) "<a>     粗纱  </a>"
    [10] => string(117) "<a>     粗纺纱  </a>"
    [11] => string(135) "<a>     粗纺羊仔纱线  </a>"
    [12] => string(117) "<a>     丝羊绒  </a>"
    [13] => string(117) "<a>     羊绒纱  </a>"
    [14] => string(118) "<a>http://zjhtfs.cn.texnet.com.cn</a>"
    [15] => string(129) "<a>@@##@@</a>"
    [16] => string(193) "<a>     <font>桐乡市</font>新拓针织有限公司  </a>"
    [17] => string(117) "<a>     卫衣布  </a>"
    [18] => string(117) "<a>     烂花布  </a>"
    [19] => string(117) "<a>     烫金布  </a>"
    [20] => string(123) "<a>     彩条汗布  </a>"
    [21] => string(111) "<a>     汗布  </a>"
    [22] => string(118) "<a>http://xtknit.cn.texnet.com.cn</a>"
    [23] => string(129) "<a>@@##@@</a>"
    [24] => string(193) "<a>     <font>桐乡市</font>玉溪针织有限公司  </a>"
    [25] => string(117) "<a>     印花布  </a>"
    [26] => string(135) "<a>     人棉氨纶汗布  </a>"
    [27] => string(111) "<a>     汗布  </a>"
    [28] => string(141) "<a>     人棉氨纶印花布  </a>"
    [29] => string(116) "<a>     罗马布  </a>"
    [30] => string(118) "<a>http://zjyuxi.cn.texnet.com.cn</a>"
    [31] => string(130) "<a>@@##@@</a>"
    [32] => string(192) "<a>     <font>桐乡市</font>洲泉众力丝织厂  </a>"
    [33] => string(129) "<a>     亚麻彩条  </a>"
    [34] => string(129) "<a>     亚麻素色  </a>"
    [35] => string(147) "<a>     亚麻烫金针织布  </a>"
    [36] => string(129) "<a>     亚麻印花  </a>"
    [37] => string(141) "<a>     人棉竹节汗布  </a>"
    [38] => string(128) "<a>http://zhonglisilk.cn.texnet.com.cn</a>"
    [39] => string(130) "<a>@@##@@</a>"
    [40] => string(207) "<a>     <font>桐乡市</font>清瀛家居用品有限公司  </a>"
    [41] => string(112) "<a>     靠垫  </a>"
    [42] => string(118) "<a>     三件套  </a>"
    [43] => string(113) "<a>     被子  </a>"
    [44] => string(113) "<a>     毯子  </a>"
    [45] => string(113) "<a>     垫子  </a>"
    [46] => string(122) "<a>http://zjqinghe.cn.texnet.com.cn</a>"
    [47] => string(130) "<a>@@##@@</a>"
    [48] => string(203) "<a>     <font>桐乡市</font>华灵丝绸有限责任公司  </a>"
    [49] => string(110) "<a>     印花  </a>"
    [50] => string(122) "<a>     长毛植绒  </a>"
    [51] => string(122) "<a>     鞋材面料  </a>"
    [52] => string(122) "<a>     服装面料  </a>"
    [53] => string(116) "<a>     仿羊绒  </a>"
    [54] => string(114) "<a>http://hlzr.cn.texnet.com.cn</a>"
    [55] => string(130) "<a>@@##@@</a>"
    [56] => string(198) "<a>     <font>桐乡市</font>晨晖丝绸有限公司  </a>"
    [57] => string(135) "<a>     大提花面料  </a>"
    [58] => string(135) "<a>     人丝美丽绸  </a>"
    [59] => string(135) "<a>     涤丝斜纹绸  </a>"
    [60] => string(129) "<a>     装饰里布  </a>"
    [61] => string(122) "<a>     小提花  </a>"
    [62] => string(128) "<a>http://chenhuisilk.cn.texnet.com.cn</a>"
    [63] => string(130) "<a>@@##@@</a>"
    [64] => string(193) "<a>     <font>桐乡市</font>嘉鸿针织有限公司  </a>"
    [65] => string(152) "<a>     彩条丝光棉珠地网眼  </a>"
    [66] => string(140) "<a>     烫银纯亚麻汗布  </a>"
    [67] => string(146) "<a>     双丝光棉彩条罗纹  </a>"
    [68] => string(134) "<a>     彩条氨纶汗布  </a>"
    [69] => string(116) "<a>     罗马布  </a>"
    [70] => string(118) "<a>http://jhknit.cn.texnet.com.cn</a>"
    [71] => string(130) "<a>@@##@@</a>"
    [72] => string(198) "<a>     <font>桐乡市</font>康泰织造有限公司  </a>"
    [73] => string(122) "<a>     丝绵绸  </a>"
    [74] => string(134) "<a>     色织双宫绸  </a>"
    [75] => string(116) "<a>     真丝  </a>"
    [76] => string(134) "<a>     涤丝双宫绸  </a>"
    [77] => string(134) "<a>     缎染双宫绸  </a>"
    [78] => string(128) "<a>http://kangtaisilk.cn.texnet.com.cn</a>"
    [79] => string(131) "<a>@@##@@</a>"
    [80] => string(205) "<a>     <font>桐乡市</font>博阳丝绸织造有限公司  </a>"
    [81] => string(111) "<a>     丝麻  </a>"
    [82] => string(111) "<a>     围巾  </a>"
    [83] => string(123) "<a>     羊绒围巾  </a>"
    [84] => string(129) "<a>     丝羊绒围巾  </a>"
    [85] => string(123) "<a>     真丝围巾  </a>"
    [86] => string(118) "<a>http://bysilk.cn.texnet.com.cn</a>"
    [87] => string(132) "<a>@@##@@</a>"
    [88] => string(203) "<a>     <font>桐乡市</font>华方纺织品有限公司  </a>"
    [89] => string(122) "<a>     毛圈布  </a>"
    [90] => string(122) "<a>     双面布  </a>"
    [91] => string(116) "<a>     汗布  </a>"
    [92] => string(128) "<a>     人棉汗布  </a>"
    [93] => string(140) "<a>     全棉竹节毛圈  </a>"
    [94] => string(126) "<a>http://huafangtex.cn.texnet.com.cn</a>"
    [95] => string(132) "<a>@@##@@</a>"
  }
}

是不是很简单,很暴力,很直接。

php免费学习视频:立即使用
踏上前端学习之旅,开启通往精通之路!从前端基础到项目实战,循序渐进,一步一个脚印,迈向巅峰!

相关文章

PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载

相关标签:

php

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

相关专题

更多
PHP 8.6报错排查指南
PHP 8.6报错排查指南

错误处理和日志记录是PHP应用定位故障、追踪运行状态的重要手段。本专题结合PHP 8.6介绍从本地调试到线上日志管理的常用做法,说明怎样保留有用的错误信息,同时控制堆栈和敏感数据的暴露,便于在问题发生后找到排查线索。

2026.12.02

20

8

PHP 8.6日期计算教程
PHP 8.6日期计算教程

日期和时间既用于页面显示,也关系到统计结果与业务规则是否准确。本专题结合PHP 8.6介绍日期计算和时区转换的常用方法,讲清日期、时间点与显示格式的区别,遇到月末、跨年或夏令时等情况时,能按明确的规则处理。

2026.12.02

60

8

PHP 8.6 cURL用法详解
PHP 8.6 cURL用法详解

cURL可用于PHP项目调用外部接口和传输文件,连接方式与认证配置会直接影响请求结果。本专题整理PHP 8.6中常见的cURL用法,说明不同连接环境下的配置思路,帮助理解证书、身份认证和请求结果之间的关系,也为调用失败时的排查提供参考。

2026.12.02

20

8

Watermill消息发布订阅实战
Watermill消息发布订阅实战

本专题聚焦Watermill的消息发布与订阅,拆解Publisher接口、Topic指定、Ack/Nack确认与批量发送,说明元数据传递、载荷序列化、消费者组与幂等消费,并整理消息重复、乱序与丢失的排查路径。

2026.12.01

20

15

Watermill安装配置指南
Watermill安装配置指南

本专题围绕Watermill的安装与快速上手展开,讲清依赖拉取、Go channel本地总线与RabbitMQ最小示例,说明消息发布订阅的调用流程、Context传递与容器网络配置,并整理依赖冲突和连接超时的处理办法。

2026.12.01

20

15

Watermill消息入门教程合集
Watermill消息入门教程合集

本专题整理Watermill的入门知识,从工具定位、核心概念到本地跑通第一条消息,讲清消息对象、目录结构与配置管理的用法,并对比其他消息库的差异,帮助Go开发者建立事件驱动的整体认知。

2026.12.01

20

15

Jest Mock 函数怎么用
Jest Mock 函数怎么用

Jest Mock 函数怎么用?本专题整理 jest.fn() 创建模拟函数、调用次数和参数断言、jest.mock() 模块替换、Spy 监视以及清理 Mock 状态等相关文章,帮助你隔离依赖并编写稳定测试。

2026.11.30

20

10

Jest 如何测试异步代码
Jest 如何测试异步代码

Jest 如何测试异步代码?本专题整理 Promise 返回值、async/await、resolves 与 rejects 断言、异步模块和定时器测试等文章,帮助你正确等待异步任务并检查成功或失败结果。

2026.11.30

20

10

Jest 的 test() 和 expect() 怎么用
Jest 的 test() 和 expect() 怎么用

Jest 的 test() 和 expect() 怎么用?本专题汇集测试用例编写、基础断言、toBe 与 toEqual 的区别,以及对象和异常断言等相关文章,帮助你掌握 Jest 单元测试的基本写法。

2026.11.30

20

10

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程