Jump to content

Get all values from img tag? width=$1 and height=$2


student101

Recommended Posts

How would I get all the values/attributes from the image(s)?

Would like to use it with PHPmailer.

 

Need to turn this:

<img border="0" hspace="2" vspace="2" align="left" width="120" height="120" alt="" src="path/to/image/image.jpg" />

into this($variables added to show what I mean by dynamic)

<img border="$1" hspace="$2" vspace="$3" align="$4" width="$5" height="$6" alt="$7" src="cid:$8">

 

Any help here would be great!

Cheers

 

 

 

Link to comment
Share on other sites

Sorry, you'll need to explain your problem a little better.

 

Is this....

 

<img border="0" hspace="2" vspace="2" align="left" width="120" height="120" alt="" src="path/to/image/image.jpg" />

 

hard coded somewhere? Where?

Link to comment
Share on other sites

Wrote something just to try. It will grab all attributes and values of the img tag(s) in a string:

 

<?php
$str = '<img border="0" hspace="2" vspace="2" align="left" width="120" height="120" alt="" src="path/to/image/image.jpg" />
Some text, another image:<img width="120" height="120" alt="" src="path/to/image/image.jpg" />
and a third<img src="path/to/image/image.jpg" alt="" />';
preg_match_all('~<img ([^>]+)>~i', $str, $matches);
$images = array();
foreach ($matches[1] as $str) {
preg_match_all('~([a-z]([a-z0-9]*)?)=("|\')(.*?)("|\')~is', $str, $pairs);
$images[] = array_combine($pairs[1], $pairs[4]);
}
echo '<pre>' . print_r($images, true) . '</pre>';
?>

Output:

Array
(
    [0] => Array
        (
            [border] => 0
            [hspace] => 2
            [vspace] => 2
            [align] => left
            [width] => 120
            [height] => 120
            [alt] => 
            [src] => path/to/image/image.jpg
        )

    [1] => Array
        (
            [width] => 120
            [height] => 120
            [alt] => 
            [src] => path/to/image/image.jpg
        )

    [2] => Array
        (
            [src] => path/to/image/image.jpg
            [alt] => 
        )

)

Link to comment
Share on other sites

Wow, thanks!

In my search for trying to figure out regular expressions, managed to get;

RegexBuddy 3,

PowerGREP,

The Regex Coach

& Regular Expressions 3rd Edition

I still have a lot of reading to get to your level.

 

Thanks so much for this thebadbad

 

Just a quick one:

Why true in print_r?

print_r (mixed $expression [, bool $return= false ])

 

 

Link to comment
Share on other sites

A true second parameter makes print_r() return its output instead of printing it. Useful when it's part of an echo construct. In my example, if the second parameter is not declared and true, the function will print its output before the echo construct and return true/1 inside the echo construct, resulting in 1 getting printed there.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.