How to regex IMG where width >= 300?

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);

Answers


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


Need Your Help

100% heigh on div element between 2 static height

html css height

I'm trying to make a div have 100% height. Which is always a pain in the ass...

Reading string with Jquery and changing check-box status according to string

jquery

I have an organized table displaying user's favorite days of the week.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.