dogfighter Posted September 30, 2008 Share Posted September 30, 2008 I've put the HTML document into a variable ($html) and now I want to pull out all images and display them in order. I tried using xpath but it was only returning the img src and not the height or width. I figure I need to match a regular expression that will get all <img src * > and put them in an array, and then recall that array with a loop. But I can't say I know HOW to do that... ??? Link to comment https://forums.phpfreaks.com/topic/126464-finding-all-images-in-a-html-document/ Share on other sites More sharing options...
Brian W Posted September 30, 2008 Share Posted September 30, 2008 why do you need height and width? if you don't include those two variables, it defaults to the image's natural size... can I see what you have so far? Link to comment https://forums.phpfreaks.com/topic/126464-finding-all-images-in-a-html-document/#findComment-653925 Share on other sites More sharing options...
discomatt Posted September 30, 2008 Share Posted September 30, 2008 I'd use a combination of strpos and regex. <pre><?php function getOffsets( $needle, $end, $haystack, $i = 0 ) { $o = array(); while( ($offset = strpos($haystack, $needle, $i)) !== FALSE ) { $o[] = array( 'start' => $offset, 'end' => strpos( $haystack, $end, $offset ) ); $i = $offset + strlen($needle); } return $o; } $html = <<<HTML <html> <head> </head> <body> <img src="test.jpg" height="15" width="20" /> <img src="test2.jpg" width="50" height="60" /> <img height="100" SRC="test3.jpg" width="90" /> <some other tags/> <with some="attributes" /> </body> </html> HTML; $arr = getOffsets( '<img', '>', $html ); foreach( $arr as $o ) { $chunk = substr( $html, $o['start'], $o['end'] - $o['start'] ); $expr = '/([a-z]++)="([^"]*+)"/i'; preg_match_all( $expr, $chunk, $attrs, PREG_SET_ORDER ); $img = array(); foreach( $attrs as $attr ) $img[ strtolower($attr[1]) ] = $attr[2]; echo "SRC: {$img['src']} \t Width: {$img['width']} \t Height: {$img['height']} <br />"; } ?></pre> Link to comment https://forums.phpfreaks.com/topic/126464-finding-all-images-in-a-html-document/#findComment-654113 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.