Using PHP regex to remove link but keep anchor text

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() function with regex.

Also, read:

Here is the complete code to remove anchors tags from a string using PHP.


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

You may also like:


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

Comments are closed.