Using regex to remove link but keep anchor text

Posted on

Sometimes we need to remove anchors from html while displaying html content. You can easily remove links or anchors from a html content using PHP preg_replace with regex. Here is the complete code to remove anchors tags from a string.


<?php
$str = 'PHPZAG PHP <a href="https://www.phpzag.com/" title="PHP">TUTORIALS</a> AND ARTICLES.';
echo preg_replace('#<a.*?>([^>]*)</a>#i', '$1', $str);
?>

One thought on “Using regex to remove link but keep anchor text

Leave a Reply

Your email address will not be published. Required fields are marked *