2016-10-13 00:16:48 点击量:345 标签: 收藏本文
/*PHP正则提取图片img标记中的任意属性*/ $str = '<center><img style="" src="/uploads/images/20100516000.jpg" height="120" width="120"><img src=/uploads/images/201005160001111.jpg height="120" width="120"><br />PHP正则提取或更改图片img标记中的任意属性</center>';
//1、取整个图片代码
preg_match_all ('/(<[img|IMG][^>]*?)src=([\'|\"])?(.*?)([\'|\"|\s])([^>]+>)/is',$str,$mach);
$str='<img border="0" src="1.jpg" alt=""/><img border="0" src="a.jpg" alt=""/><img src="test.jpg" />'; $pattern="/<[img|IMG].*?src=[\'|\"](.*?(?:[\.gif|\.jpg]))[\'|\"].*?[\/]?>/"; preg_match_all($pattern,$str,$match); //print_r($match[1]); print_r($match);
// 获取字符串里所有图片的正则表达式
$str='liwsen<img alt="" src="/images/hh/02.gif" />dfds<img alt="" src="/img/ad.gif" />dhh呵呵';
preg_match_all("/<img.*\>/isU",$str,$ereg);
foreach($ereg[0] as $cc => $image){
echo $image."<br>";
}
// 获取字符串里所有超链接的正则表达式
$str="<a href='http://www.tjgad.com'>天津港文化传媒</a>啊哈哈<a href='http://www.sina.com'>新浪</a>啊哈哈<a href='http://www.baidu.com'>百度</a>tjgad";
preg_match_all("/<a[[:space:]]*href.*\>.*\<\/a\>/isU",$str,$ereg);
foreach($ereg[0] as $cc => $lnk){
echo $lnk."<br>";
}