加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php正则表达式提取图片地址 超链接 标题 实现代码

发布时间:2020-12-13 20:39:44 所属栏目:PHP教程 来源:网络整理
导读:正则表达式提取网址中的id,代码如下: preg_match( /http://t.sina.com.cn/(d+)/fans// , $html , $result );//正则表达式提取网址中的id echo $result [1]; PHP的正则表达式提取图片地址的代码,代码如下: $str = pstyle=padding:0px;margin-top:0px;margin-b

正则表达式提取网址中的id,代码如下:

  1. preg_match('/http://t.sina.com.cn/(d+)/fans//',$html,$result); //正则表达式提取网址中的id 
  2. echo $result[1]; 

PHP的正则表达式提取图片地址的代码,代码如下:

  1. $str='<p style="padding: 0px; margin-top: 0px; margin-bottom: 0px; line-height: 200%;"><img border="0" src="upfiles/2009/07/1246430143_4.jpg" alt=""/></p><p style="padding: 0px; margin-top: 0px; margin-bottom: 0px; line-height: 200%;"><img border="0" src="upfiles/2009/07/1246430143_3.jpg" alt=""/></p><p style="padding: 0px; margin-top: 0px; margin-bottom: 0px; line-height: 200%;"><img border="0" src="upfiles/2009/07/1246430143_1.jpg" alt=""/></p>';  $pattern="/<[img|IMG].*?src=['|"](.*?(?:[.gif|.jpg]))['|"].*?[/]?>/";  preg_match_all($pattern,$str,$match);  print_r($match); 

PHP正则表达式提取超链接及其标题,代码如下:

  1. $str = '<a id="top8" href="http://list.MP3.baidu.com/song/A.htm?top8" class="p14" target="_top">歌曲列表</a><br><a target="_blank" id="bp" href="http://list.mp3.baidu.com/list/bangping.html" class="p14">中文金曲榜</a><br><td nowrap="nowrap">&#8226;&nbsp;<a id="top19" href="qingyinyue.html?top19" class="p14" target="_top">轻音乐</a></td>'
  2. 利用正则表达式是最简单的,其它的办法,偶米去想。。。 
  3. $pat = '/<a(.*?)href="(.*?)"(.*?)>(.*?)</a>/i'
  4. preg_match_all($pat$str$m); 

输出方法:print_r($m[2]);print_r($m[4]);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读