PHP正则获取HTML里需要的数据

1、使用的函数:

  • preg_match()匹配单个
  • preg_match_all()匹配所有

2、代码:

/*** @todo 正则获取HTML中想要的数据
*/
public function regexAction(){//$text=file_get_contents('http://www.***.***/');$text = $this->getHtml();//preg_match_all('# <a\b[^>]*\bdata-code=([^\s>]+)[^>]*>[\s\S]*?([^<>]*)</a>#i', $text, $match);  //匹配 [data-code=] 里面的内容,包括""preg_match_all('# <a\b[^>]*\bdata-code="([^\s>]+)"[^>]*>[\s\S]*?([^<>]*)</a>#i', $text, $match); //匹配 [data-code=""] 括号里面的内容preg_match_all('#<span>([^<]*)</span>#',$text,$match1);  //匹配span标签里面的内容preg_match_all ('/<a href=\"(.*?)\".*?>(.*?)<\/a>/i',$text,$match2);    //匹配所有的a标签echo '<pre>';print_r($match);print_r($match1);print_r($match2);echo '</pre>';die;
}function getHtml(){$html = '<a href="">A标签1哦</a>'.'<a href="">A标签2哦</a>'.'<a href="">A标签3哦</a>'.'<dl class="dl-bg"><dt>A</dt><dd><a href="https://www.kuaidi100.com/all/axd.shtml" id="a-anxindakuaixi" data-code="anxindakuaixi"><span>安信达</span></a><a href="https://www.kuaidi100.com/all/auspost.shtml" id="a-auspost" data-code="auspost" style="width:110px;"><span>澳大利亚邮政</span></a></dd></dl><dl><dt>B</dt><dd><a href="https://www.kuaidi100.com/all/htky.shtml" id="a-huitongkuaidi2" data-code="huitongkuaidi"><span>百世快递</span></a><a href="https://www.kuaidi100.com/all/yzgn.shtml" id="a-youzhengguonei2" data-code="youzhengguonei"><span>包裹平邮</span></a><a href="https://www.kuaidi100.com/all/bangsongwuliu.shtml" id="a-bangsongwuliu" data-code="bangsongwuliu"><span>邦送物流</span></a></dd></dl><dl class="dl-bg"><dt>D</dt><dd><a href="https://www.kuaidi100.com/all/dhl.shtml" id="a-dhl" data-code="dhl"><span>DHL快递</span></a><a href="https://www.kuaidi100.com/all/dtwl.shtml" id="a-datianwuliu" data-code="datianwuliu"><span>大田物流</span></a><a href="https://www.kuaidi100.com/all/dbwl.shtml" id="a-debangwuliu2" data-code="debangwuliu"><span>德邦快递</span></a></dd></dl><dl><dt>E</dt><dd><a href="https://www.kuaidi100.com/all/ems.shtml" id="a-ems2" title="EMS国内" data-code="ems"><span>EMS国内</span></a><a href="https://www.kuaidi100.com/all/emsguoji.shtml" id="a-emsguoji" title="EMS国际" data-code="emsguoji"><span>EMS国际</span></a><a href="https://www.kuaidi100.com/all/ems.shtml" id="a-ems3" title="E邮宝" data-code="ems"><span>E邮宝</span></a></dd></dl><dl class="dl-bg"><dt>F</dt><dd><a href="https://www.kuaidi100.com/all/rufengda.shtml" id="a-fedex2" title="凡客配送" data-code="rufengda"><span>凡客配送</span></a><a href="https://www.kuaidi100.com/all/fedex.shtml" id="a-fedex" title="fedex快递查询" data-code="fedex" style="width:110px;"><span>FedEx(国际件)</span></a></dd></dl><dl><dt>G</dt><dd><a href="https://www.kuaidi100.com/all/guotongkuaidi.shtml" id="a-guotongkuaidi" data-code="guotongkuaidi"><span>国通快递</span></a><a href="https://www.kuaidi100.com/all/yzgj.shtml" id="a-youzhengguonei" data-code="youzhengguonei"><span>挂号信</span></a><a href="https://www.kuaidi100.com/all/gongsuda.shtml" id="a-gongsuda" data-code="gongsuda"><span>共速达</span></a><a href="https://www.kuaidi100.com/all/yzgj.shtml" id="a-youzhengguoji" data-code="youzhengguoji"><span>国际小包</span></a></dd></dl><dl class="dl-bg"><dt>H</dt><dd><a href="https://www.kuaidi100.com/all/tdhy.shtml" id="a-tiandihuayu2" data-code="tiandihuayu"><span>华宇物流</span></a></dd></dl><dl><dt>J</dt><dd><a href="https://www.kuaidi100.com/all/jjwl.shtml" id="a-jiajiwuliu" title="佳吉物流" data-code="jiajiwuliu"><span>佳吉快运</span></a><a href="https://www.kuaidi100.com/all/jywl.shtml" id="a-jiayiwuliu" title="佳怡物流查询" data-code="jiayiwuliu"><span>佳怡物流</span></a><a href="https://www.kuaidi100.com/all/canpost.shtml" id="a-canpost" data-code="canpost"><span>加拿大邮政</span></a></dd></dl><dl class="dl-bg"><dt>K</dt><dd><a href="https://www.kuaidi100.com/all/kjkd.shtml" id="a-kuaijiesudi" title="快捷快递" data-code="kuaijiesudi"><span>快捷速递</span></a></dd></dl><dl><dt>L</dt><dd><a href="https://www.kuaidi100.com/all/lb.shtml" id="a-longbanwuliu" title="龙邦速递" data-code="longbanwuliu"><span>龙邦速递</span></a><a href="https://www.kuaidi100.com/all/lianb.shtml" id="a-lianbangkuaidi" title="联邦快递查询" data-code="lianbangkuaidi"><span>联邦快递</span></a><a href="https://www.kuaidi100.com/all/lhtwl.shtml" id="a-lianhaowuliu" title="联昊通物流" data-code="lianhaowuliu"><span>联昊通</span></a></dd></dl><dl class="dl-bg"><dt>N</dt><dd><a href="https://www.kuaidi100.com/all/ndkd.shtml" id="a-ganzhongnengda" data-code="ganzhongnengda"><span>能达速递</span></a></dd></dl><dl class="dl-bg"><dt>Q</dt><dd><a href="https://www.kuaidi100.com/all/qy.shtml" id="a-quanyikuaidi" data-code="quanyikuaidi"><span>全一快递</span></a><a href="https://www.kuaidi100.com/all/qjt.shtml" id="a-quanfengkuaidi" title="全峰快递单号查询"  data-code="quanfengkuaidi"><span>全峰快递</span></a><a href="https://www.kuaidi100.com/all/qrt.shtml" id="a-quanritongkuaidi" data-code="quanritongkuaidi"><span>全日通</span></a></dd></dl><dl><dt>S</dt><dd><a href="https://www.kuaidi100.com/all/st.shtml" id="a-shentong2" title="申通快递单号查询" data-code="shentong"><span>申通快递</span></a><a href="https://www.kuaidi100.com/all/sf.shtml" id="a-shunfeng2" title="顺丰快递查询" data-code="shunfeng"><span>顺丰快递</span></a><a href="https://www.kuaidi100.com/all/sewl.shtml" id="a-suer" title="速尔物流" data-code="suer"><span>速尔快递</span></a></dd></dl><dl class="dl-bg"><dt>T</dt><dd><a href="https://www.kuaidi100.com/all/tnt.shtml" id="a-tnt" data-code="tnt"><span>TNT快递</span></a><a href="https://www.kuaidi100.com/all/tt.shtml" id="a-tiantian2" title="海航天天快递" data-code="tiantian"><span>天天快递</span></a><a href="https://www.kuaidi100.com/all/tdhy.shtml" id="a-tiandihuayu" data-code="tiandihuayu"><span>天地华宇</span></a></dd></dl><dl><dt>U</dt><dd><a href="https://www.kuaidi100.com/all/ups.shtml" id="a-ups" data-code="ups"><span>UPS快递</span></a><a href="https://www.kuaidi100.com/all/usps.shtml" id="a-usps" data-code="usps" style="width:120px;"><span>USPS(美国邮政)</span></a></dd></dl><dl class="dl-bg"><dt>X</dt><dd><a href="https://www.kuaidi100.com/all/xbwl.shtml" id="a-xinbangwuliu"  data-code="xinbangwuliu"><span>新邦物流</span></a><a href="/all/newegg.shtml" id="a-newegg"  data-code="neweggozzo"><span>新蛋物流</span></a><a href="/all/hkpost.shtml" id="a-hkpost"  data-code="hkpost"><span>中国香港邮政</span></a></dd></dl><dl><dt>Y</dt><dd><a href="https://www.kuaidi100.com/all/yt.shtml" id="a-yuantong2" title="圆通快递查询单号"  data-code="yuantong"><span>圆通快递</span></a><a href="https://www.kuaidi100.com/all/yd.shtml" id="a-yunda2" title="韵达快递单号查询"  data-code="yunda"><span>韵达快递</span></a><a href="https://www.kuaidi100.com/all/yzgn.shtml" id="a-youzhengguonei3" data-code="youzhengguonei"><span>邮政包裹</span></a><a href="https://www.kuaidi100.com/all/yskd.shtml" id="a-youshuwuliu" title="优速快递"  data-code="youshuwuliu"><span>优速快递</span></a></dd></dl><dl class="dl-bg"><dt>Z</dt><dd><a href="https://www.kuaidi100.com/all/zt.shtml" id="a-zhongtong2" data-code="zhongtong"><span>中通快递</span></a><a href="https://www.kuaidi100.com/all/ztky.shtml" id="a-zhongtiewuliu"  title="中铁快运" data-code="zhongtiewuliu"><span>中铁快运</span></a><a href="https://www.kuaidi100.com/all/zjs.shtml" id="a-zhaijisong2" data-code="zhaijisong"><span>宅急送</span></a><a href="https://www.kuaidi100.com/all/zywl.shtml" id="a-zhongyouwuliu" data-code="zhongyouwuliu"><span>中邮物流</span></a></dd></dl>';return $html;
}

PHP正则获取HTML里需要的数据相关推荐

  1. js获取对象里数据条数

    js获取对象里数据条数 对象无法使用length获取长度,需要使用js原生方法里的Object.keys方法进行获取,具体用法如下: let obj = {'a1': {'name': '张三''}, ...

  2. 小程序js中data获取服务器数据,微信小程序 获取javascript 里的数据

    微信小程序 获取javascript 里的数据 wxml如何获取js里的数据 例: wxml里: {{txt}} 通过上面的{{txt}}可以对应获取js里data下定义的txt的值 js里: dat ...

  3. 在前台或会员中心获取表单向导里提交的数据

    2019独角兽企业重金招聘Python工程师标准>>> v9_form_consult为你的表单数据表,`consultman`,`question`,`username`,`dat ...

  4. python爬取网页上的特定链接_python3下scrapy爬虫(第三卷:初步抓取网页内容之抓取网页里的指定数据)...

    上一卷中我们抓取了网页的所有内容,现在我们抓取下网页的图片名称以及连接 现在我再新建个爬虫文件,名称设置为crawler2 做爬虫的朋友应该知道,网页里的数据都是用文本或者块级标签包裹着的,scrap ...

  5. excel层级数据获取_量化获取股票市场及金融财经数据到Excel

    本文介绍一种免费的快速获取金融市场数据的量化方法,适用于广大投资者,整个流程需要一台上网的电脑,有Excel软件.不过需要中上等的电脑操作水平,如果平时写个Word文章制作个Excel列表都不熟练的话 ...

  6. 使用 selector 从 SAP Spartacus state 里读取 Cart 数据

    选择器 selector 是用于获取存储状态 state 切片的纯函数. @ngrx/store 提供了一些帮助函数来优化这个选择. 选择器在选择状态切片时提供了许多功能. 使用 createSele ...

  7. 在SAP分析云里根据业务数据绘制词云(Word Cloud)

    词云(WordCloud)的概念,据网络上搜索到的资料,最早由美国一位新闻学专业的副教授提出,对文本中出现频率较高的关键词予以视觉上的突出,一般采取"出现频次越高,关键词渲染尺寸越大&quo ...

  8. Python获取2019-nCoV疫情实时追踪数据

    戳蓝字"CSDN云计算"关注我们哦! 来源 | 三行科创 责编 | Carol 出品 | CSDN云计算(ID:CSDNcloud) 2019-nCoV新型冠状病毒引发的肺炎牵动全 ...

  9. springboot获取客户端发来的数据

    springboot请求处理 请求映射 package com.atguigu.boot.controller; ​ ​ import org.springframework.ui.Model; im ...

  10. Java正则获取a标签href_获取页面所有a标签href

    怎样获取页面中所有带href属性的标签集合 使用: document.links document.links instanceof HTMLCollection; 注意: 1. a 标签和 area ...

最新文章

  1. 客户端AJAX验证表单
  2. python实现http下载文件-Python HTTP下载文件并显示下载进度条功能的实现
  3. 【图像】插值方法原理(最近邻,双线性,双三,兰索斯)
  4. leetcode-54 螺旋矩阵
  5. Tcl与Design Compiler (十二)——综合后处理
  6. scala中:: , +:, :+, :::, +++的区别
  7. 介绍Spring Cloud Stream与RabbitMQ集成
  8. 苹果cmsV10仿哈哩哈哩动漫自适应模板
  9. DHCP+VTP(实验讲解+配置)
  10. 连接oracle报错:Invalid connection string format, a valid format is: host:port:sid
  11. 今天参加广州.NET俱乐部活动,运气超好
  12. 【MODBUS】组态王通过串口与MODBUS RTU设备通讯
  13. 桌面壁纸被计算机管理员禁用,Win7更改桌面壁纸时出现“此功能已被禁用”如何解决...
  14. 读取视频文件python-opencv
  15. [MvcCore]记录部署好的应用程序突然无法访问的一次蹩脚经历!
  16. 国内首部创业纪实电影《燃点》进校园:如何靠近成功的2%...
  17. DS18B20温度采集报警系统,原理及汇编、C语言实现
  18. c语言余弦函数,C语言编写余弦函数曲线
  19. oracle 菜单不见了,开始菜单不见了怎么办 开始菜单不见了解决方法【详解】
  20. 国家职业资格目录(执业资格,从业资格,职称)与各类国家证书

热门文章

  1. 安徽省月度降水量分布数据
  2. 漫谈Clustering:高斯混合模型(GMM)
  3. java hibernate 插入数据_hibernate 批量插入数据
  4. 错排公式的推导及应用
  5. dns修复工具一键修复_ps污点修复画笔工具怎么用?修复工具组(一)
  6. 面部捕捉技术_为什么选择魔神运动捕捉系统?
  7. php mysql delete_PHP MySQL Delete
  8. 浏览器 重定向次数限制_临近年关,修复ASP.NET Core因浏览器内核版本引发的单点登录故障...
  9. 不提交怎么获取input中的值并存储_快速读懂innodb存储引擎
  10. 轻云,云虚拟,ECS差别