Jump to content

Get the array values into img tags?


student101

Recommended Posts

Posted by; thebadbad

<?php
$str = '<img border="0" hspace="2" vspace="2" align="left" width="120" height="120" alt="image" 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>';
?>

Some output...

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

 

How do I get the array values into this:

<img border="$arr0" hspace="$arr1" vspace="$arr2" align="$arr3" width="$arr4" height="$arr5" alt="$arr6" src="$arr7" />

(based on the fact the image could be in any of these formats)...

<img name="newsletter/FCKeditor/uploadfiles/image/8ball.jpg" src="" width="64" height="64" alt="8ball">

<img alt="" width="64" height="64" src="newsletter/FCKeditor/uploadfiles/image/8ball.jpg" />

<img border="0" hspace="0" align="left" width="64" height="64" alt="8ball" src="newsletter/FCKeditor/uploadfiles/image/8ball.jpg" />

<img border="0" hspace="2" vspace="2" align="absMiddle" width="64" height="64" alt="" src="newsletter/FCKeditor/uploadfiles/image/8ball.jpg" />

$mail->AddEmbeddedImage('FCKeditor/uploadfiles/image/8ball.jpg', '8ball2', '8ball.jpg');
$mail->Body = 'Embedded Image: <br>
<img name="ball" src="cid:8ball" width="64" height="64" alt="8ball">
<br>
Embedded Image 2: <img name="8ball2" src="cid:8ball2" width="64" height="64" alt="8ball2">';

 

Will the array go according to the array value, ie: width=array['width']

 

 

Link to comment
https://forums.phpfreaks.com/topic/169767-get-the-array-values-into-img-tags/
Share on other sites

Can you set the img tag to a string?  If you can, you could search the string for the attributes that need to be replaced and fill them in with the appropriate value.

 

For instance, if you were looking to populate the name attribute, you could search the string looking for src=""' and then replace it with 'src=$arr7'.

 

Hope this helps.  :D

All you need to do is to loop through the array of $images to regenate the <img /> tag, like so

foreach($images as $image)
{
    echo '<img';

    foreach($image as $attr_name => $attr_value)
    {
        echo " $attr_name=\"$attr_value\"";
    }

    echo ' />';
}

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.