-1

I am making a method to resize an image from wordpress.

I need to remove a value from a string eg:

<img src="/path/img.jpg" width="600" height="400" />

I need to remove the 600 and 400 values and place into my function like so:

resiseImg($width, $height);

I will then return the width and height and perform a str_replace. However I am unsure as to how I will actually take them out of the string, I guess an ugly way could be to perform some form of binary search algorithm of the string, which I really don't want to do unless there is no other option.

lorem monkey
  • 3,942
  • 3
  • 35
  • 49
zomboble
  • 835
  • 2
  • 10
  • 23

2 Answers2

3

You should avoid using RegEx for this task since you are more concerned with getting "something" meaningful out of HTML, an attribute i.e, you are parsing NOT matching. For this DOMDocument should be the ideal choice.

$fragment = new DOMDocument();
$fragment->loadHTML(  YOUR_HTML );
$image = $fragment->getElementsByTagName("iframe")->item(0);
print $image->getAttribute('width');
print $image->getAttribute('height');

The obligatory: The cannot hold before its too late.

Community
  • 1
  • 1
Shubham
  • 21,300
  • 18
  • 66
  • 89
-3
php > $a = '<img src="blahh" width="600px" height="400px" />';
php > print preg_match_all('/[0-9]+/',$a,$res);
2
php > print_r($res);
Array
(
    [0] => Array
        (
            [0] => 600
            [1] => 400
        )
)
matteomattei
  • 650
  • 5
  • 9