Home > Back-end >  How to remove anchor tag with a specific link in it?
How to remove anchor tag with a specific link in it?

Time:06-11

I have an anchor tag

<a href="http://abc.com.search.php?searctext=test"> Remove anchor tag</a>

I want to remove the anchor tag but want to retain the text within it? how do i do it

CodePudding user response:

please try this

 <?php
    
    $stripped=strip_tags('<a href="http://abc.com.search.php?searctext=test">       Remove anchor tag</a>');
    echo $stripped;
    ?>

CodePudding user response:

$str = '
<div>
  <a href="http://abc.com.search.php?searctext=test">Remove anchor tag</a>
  <a href="http://abc.com.search.php?searctext=test">Another tag which will stay</a>
</div>
';

$pattern = '~(.*)(<a href="http://abc.com.search.php\?searctext=test">)  (Remove anchor tag)  (<\/a>)(.*)~';

$replacement = '$1<span>$3</span>$5';

$result = preg_replace($pattern, $replacement, $str);

echo $result;
  • Related