Jump to content

Finding all images in a HTML document


dogfighter

Recommended Posts

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

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>

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.