Remove link but keep the anchor text using regular expression

Sometimes, especially when using some sort of CMS, you want to alter the output of some build in function i.e. remove the link. Here's how to do it.

Just a normal link like this:

Lorem Ipsum

Now we want to get rid of the link around the “Lorem Ipsum”. We’re using preg_replace() to do that

$str = 'Lorem Ipsum'; 
echo preg_replace('#([^#i', '$1', $str); 

If you’re using WordPress you might, for example, want to get the terms the post is assigned to, but you don’t want the results to be links.

$my_terms = get_the_term_list($post->ID, $my_taxes);
echo preg_replace('#([^#i', '$1', $my_terms); 

Club-Mate, the beverage →