0

I'm not very good with regex so I'm hoping to get a bit of help with this one. Basically I need to remove images that are inserted from a specific domain in a string. The string could contain images from other domains, that need to remain, and text that needs to stay too.

I have tried using regex like so (fairly sure I'm still way off though):

$content = preg_replace("/http://www.theblacklisteddomain.com[^>]+\>/i", " ", $content); 

I'm open to other solutions outside of regex that don't involve loading a library. It needs to happen on the server side, I know that with jQuery or vanilla JS this would be a lot easier.

Thanks!

BarryWalsh
  • 1,300
  • 4
  • 14
  • 36
  • You want to replace the url or the full html `` tag ? – soyuka Mar 12 '13 at 16:49
  • The full image tag including the src. Basically strip out any images inserted from a domain. The exact URL from the domain can vary based on the path of the image. – BarryWalsh Mar 12 '13 at 16:54
  • Edited, but it's not a [good way parsing html](http://stackoverflow.com/questions/3577641/how-to-parse-and-process-html-xml-with-php). – soyuka Mar 12 '13 at 17:07

1 Answers1

1

You need to escape the / chars like this :

 $content = preg_replace('/<img.*src="http:\/\/www.theblacklisteddomain.com(.*?)".*\/?>/', ' ', $content);

See the ideone example.

By the way it's not a good way parsing html.

Community
  • 1
  • 1
soyuka
  • 8,839
  • 3
  • 39
  • 54
  • Thank you! I just realised there was a better way to solve the problem in my situation. Basically it was a 1px x 1px image getting inserted. So the solution is to just check the size of the image. Thanks for your help! – BarryWalsh Mar 12 '13 at 17:59