正则表达式排除不管用啊,各位看看我这个写的有什么错误

yingyouzai 2009-10-29 08:36:34
我的意思是,替换<a ...>a</a> 为空,但是如果 a 标签内部有<img 的话就不替换了
比如 $content = "asdfasdfasdf<a href=><img ></a>asdfasdf"; 这个就不替换
$content = "asdfasdfasdf<a href=></a>asdfasdf"; 这个就替换掉
但是我这么写不行,有img标签也给替换掉了


$content = "asdfasdfasdf<a href=><img ></a>asdfasdf";
$content = preg_replace("/<a.*?[^img]<\/a>/i", "", $content);
echo $content;
...全文
79 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
wcqqq 2009-10-30
  • 打赏
  • 举报
回复
<?php 

$str = "<a href='casd' > scay </a> <a href='casd' > <img />cay </a>\n<a href='casd' > scay </a> <a href='casd' > <img />cay </a>";

$str_s = preg_replace('/<a[^>]+>(?![^<>]+<img).*?<\/a>/im' , '' , $str);

echo htmlspecialchars($str_s);

?>
wcqqq 2009-10-30
  • 打赏
  • 举报
回复
<?php 

$str = "<a href='casd' > scay </a> <a href='casd' > <img />cay </a>\n<a href='casd' > scay </a> <a href='casd' > <img />cay </a>";
[code=PHP]

$str_s = preg_replace('/<a[^>]+>(?![^<>]+<img).*?<\/a>/im' , '' , $str);

echo htmlspecialchars($str_s);

?>
[/code]
cayleung 2009-10-29
  • 打赏
  • 举报
回复

$str = "<a href='casd' >cay </a> <a href='casd' ><img />cay </a> ";
function imgcheck($str){
return preg_match("/<img[^>]+?>/",$str[0])?$str[0]:'';
}
echo preg_replace_callback("/(<a[^>]+?>.+?<\/a>)/","imgcheck",$str);

php是非常强大的~~~
yunfeifan 2009-10-29
  • 打赏
  • 举报
回复
preg_replace("/<a[^>]+>(?!<img).*<\/a>/i", "", $content);
yingyouzai 2009-10-29
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 zhoupuyue 的回复:]
试一下这个:
PHP code$content=preg_replace("/<a.*?(?!img)<\/a>/i","",$content);

[/Quote]
不行,也是全都替换掉了
阿_布 2009-10-29
  • 打赏
  • 举报
回复
试一下这个:


$content = preg_replace("/<a.*?(?!img)<\/a>/i", "", $content);


21,891

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧