student101 Posted August 11, 2009 Share Posted August 11, 2009 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'] Quote Link to comment https://forums.phpfreaks.com/topic/169767-get-the-array-values-into-img-tags/ Share on other sites More sharing options...
sroberge Posted August 11, 2009 Share Posted August 11, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/169767-get-the-array-values-into-img-tags/#findComment-895773 Share on other sites More sharing options...
wildteen88 Posted August 11, 2009 Share Posted August 11, 2009 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 ' />'; } Quote Link to comment https://forums.phpfreaks.com/topic/169767-get-the-array-values-into-img-tags/#findComment-895780 Share on other sites More sharing options...
student101 Posted August 11, 2009 Author Share Posted August 11, 2009 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. It gives me more insight to this problem I have. Thank you! Quote Link to comment https://forums.phpfreaks.com/topic/169767-get-the-array-values-into-img-tags/#findComment-895948 Share on other sites More sharing options...
student101 Posted August 11, 2009 Author Share Posted August 11, 2009 All you need to do is to loop through the array of $images to regenate the <img /> tag This looks promising. Thank you! Quote Link to comment https://forums.phpfreaks.com/topic/169767-get-the-array-values-into-img-tags/#findComment-895949 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.