Unlink specific Links in string

Hello, I have the following code that unlinks all of the links in the string. How can I specify it to remove only the links with the http://law.mysite.com/attorneys/david-airan URL? So in this example Google will still be linked. Thanks

$text = '<p><a href="http://law.mysite.com/attorneys/david-airan">David M. Airan</a></p>
<p><a href="http://law.mysite.com/attorneys/david-airan">David Airan</a></p>
<p><a href="http://law.mysite.com/attorneys/david-airan">Dave Airan</a></p>
<p><a href="http://www.google.com">Google</a></p>';

echo preg_replace('/<a href=\"(.*?)\">(.*?)<\/a>/', "\\2", $text);

I would expect that you’d extend the starting-specification

'/<a href=\"

to include the links on your own site, but I’m not sure of the syntax and how you might have to escape any characters. I read that as searching for a string that starts with the open-href link as far as the opening double-quote, so you need to extend that to also include your domain, so something like

'/<a href=\"http://law.mysite.com

would be needed to match anything starting with your domain. But I don’t know if you have to escape the forward-slashes, full-stops and so on. And that would only work for your specific example, to unlink stuff that specifically matches your domain - I imagine there’s a way to unlink any link that isn’t Google, if that’s what you meant.

Ultimately, I need a script that will find any links in a string with a specified URL and remove the link, but keep the anchor text.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.