0

I need to remove <a href from text, except if domain/url in href contains example.com

I have found this regex, but it replaces right now all urls (.*?) , any ideas how to make it so it replaces all hrefs except if url contains example.com

preg_replace('/<a href=\"(.*?)\">(.*?)<\/a>/', "\\2", $_POST['desc']);

Here is example how the replace should work

<a href="http://example.com/test">test1</a> ==> Should not be replaced (targeted) <a href="http://google.com/test">test2</a> ==> test2

bombix
  • 1

1 Answers1

-1
['"]https?:\/\/(?!example\.com)[^"']+['"]

That regex will match all URLs exept those whose domain is example.com In this case i'm asuming it may be encapsulated either between single quotes or double quotes, say href="{url}" or href='{url}'.

Eduardo Escobar
  • 3,301
  • 2
  • 18
  • 15