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
  • 91,102
  • 13
  • 100
  • 152
yuli chika
  • 9,053
  • 20
  • 75
  • 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
  • 811,555
  • 193
  • 1,581
  • 1,452
0
"/<img[^>]*width=\"[0-9]*[3-9][0-9]{2}\"[^>]*\/>/"
aleph_null
  • 5,766
  • 2
  • 24
  • 39