cj333
cj333

Reputation: 2609

php get image width from img style attr

how to get image width and height from img style? for example:

$str = '<img style="margin-right:10px;width:37px;border:0;" src="icons/icon.png">';

How to get width => 37px?

I think if we write a width in a style attr, we could write it like these:

style="width:37px;"(no space with semicolon)
style="width: 37px;"(space with semicolon)
style="width:37px"(no space no semicolon)
style="width: 37px"(space no semicolon)

if no semicolon, the width must be write in the end of the style, like style="height:25px;width:37px"

so how to do it more easier? regex or dom? Thanks.

Upvotes: 1

Views: 4199

Answers (1)

Toddish
Toddish

Reputation: 516

$image = '<img src="" style="border-width: 10px; width: 32px;">';
preg_match('/[^-]width[: ]+([0-9]+)/', $image, $matches);
print_r($matches);

$matches[1] should have your answer, and this'll only work if you only pass in the img string, otherwise it'll pick up other element widths.

Upvotes: 4

Related Questions