干货来了,PHP截取网页源码内所有<a>标签,并形成数组

前言

        如果我们需要获取一个网页的源码,并从中获取一些自己需要的信息,比如<a>标签,或者<p><ul>标签当中的所有内容和的时候。

        我们就必须要截取,然而php截取函数,相当的操蛋,原谅博主是个新手,至少我找了很久,并没有发现有向左截取字符串的函数

        像别人的答案substr()配合获取字符串位置的函数,然后截取。

        我只能说试过,并不好用。

        然后还有些七七八八的办法,统统不实用,麻烦。

        接下来我就要说一种方法了,相当的BUG,相当的好用。


翠花,上代码!!!


---------------------------------------------说正事分割线-----------------------------------------------

1,先把函数奉上吧!这里是通过将字符串打散成数组,然后截取的

function intercept_str($str,$start,$end,$option = 1){
		$strarr=explode($start,$str);
		$tem=$strarr[1];
		if(empty($end)){
		return $tem;
		}else{
		$strarr=explode($end,$tem);
		if($option==1){
		return $strarr[0];
		}
		if($option==2){
		return $start.$strarr[0];
		}
		if($option==3){
		return $strarr[0].$end;
		}else{
		return $start.$strarr[0].$end;
		}
		}
}


2,这里是函数使用具体的例子        

$url = "http://www.texnet.com.cn/product";

$result = file_get_contents($url);

$str = intercept_str($result, '粘上你要开始截取的字符串比如:<div class="productsearch">', '粘上结束时刻的字符串比如:<div 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--93
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值