高分求一个php正则表达式 本帖最后由 songle912 于 2009-08-18 18:25:05 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 $str=<<<H123 <p>abc <img border="0" src="images1.jpg"/> def <img src=images2.jpg style="border:none;"> hhh <img border='0' src='images3.jpg' style="border:none;" />hehe </p>hahaH;preg_match_all("/<img.[^>]*>/i",$str,$m);print_r(array_map("htmlentities",$m[0])); 楼上的感谢了。你写的正则我看的懂,肯定不行的,看一下我下面的说明,属性有单引号,双引号,没引号的区别。src前后还可能有其他属性。 $str='123 <p>abc <img border="0" src="images1.jpg"/> def <img src=images2.jpg style="border:none;"> hhh <img border=\'0\' src=\'images3.jpg\' style="border:none;" />hehe </p>haha';if(preg_match_all("(<img[^>]*>)", $str, $matches)){ foreach($matches[0] as $m){ echo htmlentities($m)."<br />"; } }else{ echo "Not found!"; } 我也觉得你不会扔个100分问这么简单的问题,但问题是,你要取什么内容?取img标签src属性的值 or anything? 我结果希望是: <img border="0" src="images1.jpg"/><img src=images2.jpg style="border:none;"><img border='0' src='images3.jpg' style="border:none;" />当然形式如何无所谓的,数组什么的都可以 你自己试一下,并且仔细看看,你的结果是<img src="<img border="0" src="images1.jpg"/><br /><img src=images2.jpg style="border:none;"><br /><img border='0' src='images3.jpg' style="border:none;" /><br />"/> 现在要提取其中的img标签的所有内容,结果希望是: <img border="0" src="images1.jpg"/> <img src=images2.jpg style="border:none;"> <img border='0' src='images3.jpg' style="border:none;" />这上面是你的原话啊?你要的不就是这个么?我的出来后就是<img border="0" src="images1.jpg"/><img src=images2.jpg style="border:none;"><img border='0' src='images3.jpg' style="border:none;" />啊..哪里不符合标准了??把br去掉后就和你要的完全一模一样了啊.要不你说详细点你到底要什么吧....... 难道你是因为那些"之类的???那你用这个代码吧$str='123 <p>abc <img border="0" src="images1.jpg"/> def <img src=images2.jpg style="border:none;"> hhh <img border=\'0\' src=\'images3.jpg\' style="border:none;" />hehe </p>haha';if(preg_match_all("(<img[^>]*>)", $str, $matches)){ foreach($matches[0] as $m){ echo $m; } }else{ echo "Not found!"; }然后你再查看源代码`保证对`绝对和你上面写的一模一样 $str='123 <p>abc <img border="0" src="images1.jpg"\/> def <img src=images2.jpg style="border:none;"> hhh <img border=\'0\' src=\'images3.jpg\' style="border:none;" \/>hehe <\/p>haha';preg_match_all('/<img\s+[^>]*>/is',$str,$results);print_r($results);楼主要么就是没把问题描述清楚。楼上包括我这个的正则跟你给出的要取的结果一样。你要取出所有img标签包括属性,跟你里面使用的是单引号或双引号,结尾是>或/>没关系。用正则只要描述好从<img开头到结尾>的这个范围就可以了。 preg_match_all('/<img .*jpg\"\/\>|none\"\>|none\; \/\>/i',$str, $arr);楼主试试这个,没测试, http://topic.csdn.net/u/20080411/09/16ea750b-c5ca-4573-93ae-5b8f330d782c.html 迷途IT人渴望各路高人指点!曾搞出日IP20万的网站,现在混的狗屁不是。 linux下php连不上mysql,而且php代码出错,调试的时候不报错,..谁能帮我,在线等啊.. 谁给个用gmail账号发phpmailer邮件的例子? php5 连接 mysql server 5.1 失败:1045 判断文件是否成功建立 进来看一下,散散分 讨论一下像pear:db和adodb这些数据库抽象层(发言有分) 京东上的购物车问题 文件上传,英文版(50分) 一简单问题 连接服务器问题 用phpmailer邮件类,报错请高人进,以后给补分
$str=<<<H
123 <p>abc <img border="0" src="images1.jpg"/> def <img src=images2.jpg style="border:none;"> hhh <img border='0' src='images3.jpg' style="border:none;" />hehe </p>haha
H;
preg_match_all("/<img.[^>]*>/i",$str,$m);
print_r(array_map("htmlentities",$m[0]));
$str='123 <p>abc <img border="0" src="images1.jpg"/> def <img src=images2.jpg style="border:none;"> hhh <img border=\'0\' src=\'images3.jpg\' style="border:none;" />hehe </p>haha';
if(preg_match_all("(<img[^>]*>)", $str, $matches)){
foreach($matches[0] as $m){
echo htmlentities($m)."<br />";
}
}else{
echo "Not found!";
}
取img标签src属性的值 or anything?
当然形式如何无所谓的,数组什么的都可以
<img src=images2.jpg style="border:none;">
<img border='0' src='images3.jpg' style="border:none;" />啊..哪里不符合标准了??把br去掉后就和你要的完全一模一样了啊.要不你说详细点你到底要什么吧.......
$str='123 <p>abc <img border="0" src="images1.jpg"/> def <img src=images2.jpg style="border:none;"> hhh <img border=\'0\' src=\'images3.jpg\' style="border:none;" />hehe </p>haha';
if(preg_match_all("(<img[^>]*>)", $str, $matches)){
foreach($matches[0] as $m){
echo $m;
}
}else{
echo "Not found!";
}然后你再查看源代码`保证对`绝对和你上面写的一模一样
preg_match_all('/<img\s+[^>]*>/is',$str,$results);
print_r($results);楼主要么就是没把问题描述清楚。楼上包括我这个的正则跟你给出的要取的结果一样。你要取出所有img标签包括属性,跟你里面使用的是单引号或双引号,结尾是>或/>没关系。用正则只要描述好从<img开头到结尾>的这个范围就可以了。
preg_match_all('/<img .*jpg\"\/\>|none\"\>|none\; \/\>/i',$str, $arr);楼主试试这个,没测试,