RE: preg_match_all to match <img> tags

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Maybe this is what you are searching for:

$images = array();
$data = "blah <img src=img.png width=\"400\" height='600'> src=blah.png <img
src=gg.tiff>";

preg_match_all("/\< *[img][^\>]*[.]*\>/i", $data, $matches);
foreach($matches[0] as $match)
{
	preg_match_all("/(src|height|width)*= *[\"\']{0,1}([^\"\'\ \>]*)/i",
$match, $m);
	$images[] = array_combine($m[1],$m[2]);
}

print_r($image);

It will produce:

Array 
( 
	[0] => Array 
	( 
		[src] => img.png 
		[width] => 400 
		[height] => 600 
	) 

	[1] => Array 
	( 
		[src] => gg.tiff 
	)
)

I wrote it just as an example. So you may modify it for your needs!
Does anyone know if there is a way to put this into ONE regex??

Jan

-----Original Message-----
From: brian [mailto:phplist@xxxxxxxxxxxxxxx] 
Sent: Friday, August 10, 2007 3:18 AM
To: php-general@xxxxxxxxxxxxx
Subject: Re:  preg_match_all to match <img> tags

Ólafur Waage wrote:
> I know this isn't exactly a php related question but due to the
> quality of answers ive seen lately ill give this a shot. (yes yes im
> smoothing up the crowd before the question)
> 
> I have a weblog system that i am creating, the trouble is that if a
> user links to an external image larger than 500pixels in width, it
> messes with the whole layout.
> 
> I had found some regex code im using atm but its not good at matching
> the entire image tag. It seems to ignore properties after the src
> declaration and not match tags that have properties before the src
> declaration .
> 
> preg_match_all("/\< *[img][^\>]*[src] *= *[\"\']{0,1}([^\"\'\ >]*)/i",
> $data, $matches);
> print_r($matches);
> 
> This currently makes two arrays for me, the source location from all
> img tags and a large part of the tag itself. But not the entire tag.
> 
> What i do is i match the img tag, find the src, get the image
> properties, and if the width is more than 500, i shrink it down and
> add width="X" and height="Y" properties to the image tag.
> 
> How can i match an image tag correctly so it does not cause any issues
> with how the user adds the image.
> 

<style>
#your_content_div img { max-width: 500px !important; }
</style>

OK, so it won't work with IE6. Screw them.

But if the height is set in the img tag it'll keep that, so the image 
could become distorted. So, you could also do something like:

#your_content_div img { visibility: none; }

Then run some Javascript routine onload to properly figure the 
dimensions of each image. Adjust the width down to 500px, if necessary, 
then the height by whatever percent difference between original width 
over new width:

var new_height = (original_width / 500) * original_height;

Then, whether you change the dimensions of the image or not, change the 
visibility of each to 'visible'.

So, um ... no PHP involved.

brian

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux