in Web & Tech

Regular Expression to remove links from a string with PHP

Thanks to Stu Miller for sharing this handy little regex to remove all link tags from a string with PHP:

$string = "I am a lot of text with <a href="#">links in it"</a>";
$result = preg_replace('/<a href=\"(.*?)\">(.*?)<\/a>/', "\\2", $string);
echo $result; // this will output "I am a lot of text with links in it"

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

To respond on your own website, enter the URL of your response which should contain a link to this post's permalink URL. Your response will then appear (possibly after moderation) on this page. Want to update or remove your response? Update or delete your post and re-enter your post's URL again. (Learn More)