正则表达式,用PHP从字符串中删除带有内部文本的链接

I have the following code:

$string = 'Try to remove the link text from the content <a href="#">links in it</a> Try to remove the link text from the content <a href="#">testme</a> Try to remove the link text from the content';
$string = preg_replace('#(<a.*?>).*?(</a>)#', '$1$2', $string);
$result = preg_replace('/<a href="(.*?)">(.*?)<\/a>/', "\\2", $string);
echo $result; // this will output "I am a lot of text with links in it";

I am looking to merge these preg_replace lines. Please suggest.

You need to use DOM for these tasks. Here is a sample that removes links from this content of yours:

$str = 'Try to remove the link text from the content <a href="#">links in it</a> Try to remove the link text from the content <a href="#">testme</a> Try to remove the link text from the content';
$dom = new DOMDocument;
@$dom->loadHTML($str, LIBXML_HTML_NOIMPLIED|LIBXML_HTML_NODEFDTD);
$xp = new DOMXPath($dom);
$links = $xp->query('//a');
foreach ($links as $link) {
    $link->parentNode->removeChild($link);
 }
echo preg_replace('/^<p>([^<>]*)<\/p>$/', '$1', @$dom->saveHTML());

Since the text node is the only one in the document, the PHP DOM creates a dummy p node to wrap the text, so I am using a preg_replace to remove it. I think it is not your case.

See IDEONE demo