Jump to content

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 ' />';
}

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.

Hope this helps.  :D

It gives me more insight to this problem I have.

Thank you!

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.