如何使用php从所有链接中删除href属性

How to remove a href attribute from all links from div element using php ?

Something like this:

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>';
}

I was searching for solution, but all what i found was solution with jquery

like this:

$(selector).removeAttr(attribute)

Thank you

You can user preg_replace() to achieve this. Example code:

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

You may use jQuery removeAttr() Method

Definition and Usage

The removeAttr() method removes one or more attributes from the selected elements.

$(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>

</div>

This here checks if condition is not true then echo the link else keep the link blank. Surely something as simply is this will solve your problem.

assuming outside of a php block:

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

Or in a php block:

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