-2

Been trying all manner of regex for the last couple of days, no joy.

Trying to do some regex modification on a VERY limited subset of html tags, specifically img tags but only ones like this:

<img src="picture.jpg">

and NOT ones like this:

<img src="site.com/picture.jpg">

Also those tags are mixed in with other text before and after them, so the regex needs to ignore that stuff but still work on the tags. Can't for the life of me figure it out, can anyone help?

Kevin
  • 41,694
  • 12
  • 53
  • 70
Neuron
  • 1
  • So, you wanna match all strings in the format `` where `...` is a substring that does not contain `"` or `/` characters? – Andrea Corbellini Dec 14 '14 at 06:00
  • You should not be using regex for this. Use simple-html-dom or DomDocument – pguardiario Dec 14 '14 at 06:53
  • Andrea - yep. Actually I think slashes are ok (local path) but a second period is not, this would indicate the picture was hosted somewhere else (we don't use periods in our local folder names). – Neuron Dec 14 '14 at 17:34
  • My problem is that I can't figure out a way to say "match all tags (between <> (shortest match)) that don't have more than one period in them, ignore the rest". Is that something regex by itself can do? – Neuron Dec 14 '14 at 17:36

1 Answers1

1
<?php
$test = '<img src="picture0.jpg"><img src="site.com/picture1.jpg">';

preg_match_all('/<img src="(?!site.com\/)(.*?)">/',$test,$matches);

echo '<pre>',print_r($matches),'</pre>';
Samuel Cook
  • 16,620
  • 7
  • 50
  • 62
  • Thanks but no, I don't know the site beforehand (could be any site, with any extension. I was trying to do your idea but a negative lookahead on "anything, then a period, then anything, then another period (two periods would indicate a non-local path), but it just matches everything anyways. Or matches nothing, if there are periods outside of the <> characters. – Neuron Dec 14 '14 at 17:42