0

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

5
  • What's on the div element? Commented Apr 24, 2017 at 4:28
  • Very similar, not really php, but may be useful: stackoverflow.com/questions/15364322/… Commented Apr 24, 2017 at 4:35
  • You have to use $('a').removeAttr('href'); as shown below Commented Apr 24, 2017 at 4:35
  • I need in PHP, not jquery. also when I said before, I want to remove all links from inside div element. thank you Commented Apr 24, 2017 at 4:42
  • I do not understand your question. If you have control over how the HTML id generated, you just do what you show in your code snippet. Can you clarify the problem? Commented Apr 24, 2017 at 5:34

4 Answers 4

1

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>';
Sign up to request clarification or add additional context in comments.

Comments

0

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

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

Comments

0

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>

1 Comment

But he also use $(selector).removeAttr(attribute) in his questtion
0
$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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.