Skip to main content

Using 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 with regex. Here is the complete code to remove anchors tags from a string.


<?php
$str = 'PHPZAG PHP <a href="http://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 *

Shares

Subscribe For Latest Updates

Signup for our newsletter and get notified when we publish new articles for free!