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

 

 

 

Sorry!

It's hardcoded in the FCKeditor, once you upload an image.

RED wont always be set, those wont be needed to be coded in.

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

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] => 
        )

)

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

 

 

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.