我有一个这样的字符串。
$str = "this is a bet and he is <a href=''>Mansoon</a> and he please search on <a href='http://www.google.com'>Google</a> and say he <a href=''>Yammy</a>";
我想从带有空白href的字符串中删除所有href标记。你能不能看看这个。
像这样输出
$str = "this is a bet and he is Mansoon and he please search on <a href='http://www.google.com'>Google</a> and say he Yammy";
非常感谢
答案 0 :(得分:4)
试试这个: 的更新强> 它可能会填满所有异常情况:
$re = "/(<a[^href]*href=[\"']{2}[^>]*>)([^<>]*|.*)(<\\/a>)/m";
$str = "this is a bet and he is <a id=\"sss\" href='' dfsd fdg >Mansoon</a> and he please search on <a href='http://www.google.com'>Google</a> and say he <a href=''>Yammy</a> \n<a href=''> <i>Yammy </i> <br/> </a>\n\nthis is a bet and he is Mansoon and he please search on <a href='http://www.google.com'>Google</a> and say he Yammy";
$subst = "$2";
$result = preg_replace($re, $subst, $str);
<强> live demo 强>
答案 1 :(得分:0)
此正则表达式适用于您
<a[^>]*herf='*'[^>]*>[^>](.*?)<\/a> //works well even after space anywhere
答案 2 :(得分:0)
此<a\h+href=(['"])\1>([^<>]*)<\/a>
正则表达式将匹配具有空href
值的所有锚标记。将匹配的<a>
代码替换为$2
将为您提供所需的输出。
<?php
$str = "this is a bet and he is <a href=''>Mansoon</a> and he please search on <a href='http://www.google.com'>Google</a> and say he <a href=''>Yammy</a>";
echo preg_replace('~<a\h+href=([\'"])\1>([^<>]*)<\/a>~', '$2', $str);
?>
输出:
this is a bet and he is Mansoon and he please search on <a href='http://www.google.com'>Google</a> and say he Yammy
\h+
匹配一个或多个水平空白字符。
答案 3 :(得分:0)
您可以使用:
$str = "this is a bet and he is <a href=''>Mansoon</a> and he please search on <a href='http://www.google.com'>Google</a> and say he <a href=''>Yammy</a>";
$pattern = '/<a[^>]*?href=(?:\'\'|"")[^>]*?>(.*?)<\/a>/i';
$replacement = '$1';
echo preg_replace($pattern, $replacement, $str);