如果我们需要获取一个网页的源码,并从中获取一些自己需要的信息,比如标签,或者
标签当中的所有内容和的时候。我们就必须要截取,然而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免费学习视频:立即使用
踏上前端学习之旅,开启通往精通之路!从前端基础到项目实战,循序渐进,一步一个脚印,迈向巅峰!










