如何使用PHP从所有链接中删除HREF属性



如何使用PHP从DIV元素中删除所有链接的HREF属性?

类似的东西:

if($condtion = "true"){
echo '<a href="" class="no-link">Please login</a>';
}
else{
echo '<a href="www.google.com" class="yes-link">Admin Section</a>';
}

我正在寻找解决方案,但是我发现的所有内容都是解决方案

这样:

$(selector).removeAttr(attribute)

谢谢

此处检查条件是否不正确,然后回荡链接else将链接保持空白。肯定是简单的事情,这将解决您的问题。

假设在PHP块外:

<a href="<?=(!$conditions ? 'www.google.com':'' ?>" class="yes-link">Admin Section</a>

或在PHP块中:

echo '<a href="' . (!$conditions ? 'www.google.com':''.'" class="yes-link">Admin Section</a>';

您可以用户preg_replace((实现此目的。示例代码:

$html = preg_replace('#<a class="no-link">(.*?)</a>#', '', $html);

您可以使用jquery removeattr((方法

定义和用法

removeattr((方法从所选元素中删除一个或多个属性。

$(function() {
$("a").removeAttr("href");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<div>
<a href="" > Click Me First </a><br>
<a href="" > Click Me Second </a>
</div>

$string1 = "<div><a href='a.php'>a</a><br><a href='b.php'>b</a><br><a href='c.php'>c</a><br><a href='d.php'>d</a></div>";
//$string2 = preg_replace("/'(.*?)'/i", '', $string1); //Uncomment this if you want empty the href value only
$string3 = preg_replace("/ href='(.*?)'/i", '', $string1); //Use this if you want to completely remove the href attribute with value

最新更新