从 php 字符串中删除有条件的 href 标签


Remove a href tag conditional from php string

我有这样的srting。

$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";

非常感谢

试试这个: 更新 它可以完全填充所有例外情况:

$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);

现场演示

这个<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+匹配一个或多个水平空格字符。

这个正则表达式对你有用

<a[^>]*herf='*'[^>]*>[^>](.*?)<'/a> //works well even after space anywhere

您可以使用:

$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);

测试正则表达式:这里