1

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.

4

1 回答 1

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

$matches[1] 应该有你的答案,这只有在你只传入 img 字符串时才有效,否则它将获取其他元素宽度。

于 2013-01-22T15:04:56.330 回答