student101 Posted June 10, 2009 Share Posted June 10, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/161641-get-all-values-from-img-tag-width1-and-height2/ Share on other sites More sharing options...
trq Posted June 10, 2009 Share Posted June 10, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/161641-get-all-values-from-img-tag-width1-and-height2/#findComment-852945 Share on other sites More sharing options...
student101 Posted June 10, 2009 Author Share Posted June 10, 2009 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" /> Quote Link to comment https://forums.phpfreaks.com/topic/161641-get-all-values-from-img-tag-width1-and-height2/#findComment-853037 Share on other sites More sharing options...
thebadbad Posted June 10, 2009 Share Posted June 10, 2009 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] => ) ) Quote Link to comment https://forums.phpfreaks.com/topic/161641-get-all-values-from-img-tag-width1-and-height2/#findComment-853222 Share on other sites More sharing options...
student101 Posted June 10, 2009 Author Share Posted June 10, 2009 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 ]) Quote Link to comment https://forums.phpfreaks.com/topic/161641-get-all-values-from-img-tag-width1-and-height2/#findComment-853294 Share on other sites More sharing options...
thebadbad Posted June 10, 2009 Share Posted June 10, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/161641-get-all-values-from-img-tag-width1-and-height2/#findComment-853352 Share on other sites More sharing options...
student101 Posted June 10, 2009 Author Share Posted June 10, 2009 Cool, thanks. I didn't see it documented on the print_r manual. Quote Link to comment https://forums.phpfreaks.com/topic/161641-get-all-values-from-img-tag-width1-and-height2/#findComment-853359 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.