3

How can I get all of the <img>s with a width >= 300?

My markup/code:

$images = <<<END
<img src="/data/img/201108031_023" width="300" height="400" />
<img src="/data/img/201108031_026" width="250" height="300" />
<img src="/data/img/201108031_028" width="400" height="300" />
<img src="/data/img/201108031_032" width="500" height="400" />
...
END;

My attempt:

preg_match_all("/<img(.*?) \/>/",$images,$matches);
print_r($matches);
Andy Lester
  • 86,927
  • 13
  • 98
  • 148
yuli chika
  • 8,763
  • 20
  • 74
  • 122

2 Answers2

6

It's not a good idea to use regex for this.

This works for your specific example, but it has a number of problems because HTML can't be parsed correctly by a regular expression:

"/<img[^>]*width=\"([3-9][0-9]{2}|[1-9][0-9]{3,})\"[^>]*>/"

See it working online: ideone

I'd suggest you look for an HTML parser instead.

Related

Community
  • 1
  • 1
Mark Byers
  • 767,688
  • 176
  • 1,542
  • 1,434
0
"/<img[^>]*width=\"[0-9]*[3-9][0-9]{2}\"[^>]*\/>/"
aleph_null
  • 5,746
  • 2
  • 23
  • 37