当前位置:编程学习 > php >>

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

正则表达式提取网址中的id

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


PHP的正则表达式提取图片地址的代码。

 代码如下 复制代码
$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正则表达式提取超链接及其标题

 

 代码如下 复制代码

$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">• <a id="top19" href="qingyinyue.html?top19" class="p14" target="_top">轻音乐</a></td>';

利用正则表达式是最简单的,其它的办法,偶米去想。。。

$pat = '/<a(.*?)href="(.*?)"(.*?)>(.*?)</a>/i';
preg_match_all($pat, $str, $m);

输出方法:

print_r($m[2]);
print_r($m[4]);

补充:Php教程,Php应用 
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,