Re: [PHP] preg_match_all to match img tags

2007-08-11 Thread tedd

At 11:45 PM + 8/9/07, Ó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.


You could set a class attribute for the image in 
html and scale it in css. An img {width: 100%;} 
would hold the size to the maximum of the parent.


Cheers,

tedd
--
---
http://sperling.com  http://ancientstones.com  http://earthstones.com

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



Re: [PHP] preg_match_all to match img tags

2007-08-11 Thread Richard Heyes

How can i match an image tag correctly so it does not cause any issues
with how the user adds the image.


preg_match_all('/img[^]*/Ui');

Off the top of my head. This wouldn't allow for using the right angle 
bracket in the img tag, but that's almost never going to happen in reailty.


--
Richard Heyes
+44 (0)844 801 1072
http://www.websupportsolutions.co.uk

Knowledge Base and HelpDesk software
that can cut the cost of online support

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



Re: [PHP] preg_match_all to match img tags

2007-08-11 Thread Richard Heyes

Richard Heyes wrote:

How can i match an image tag correctly so it does not cause any issues
with how the user adds the image.


preg_match_all('/img[^]*/Ui');

Off the top of my head. This wouldn't allow for using the right angle 
bracket in the img tag, but that's almost never going to happen in reailty.


Oops that should be:

preg_match_all('/img[^]*/Ui', $input, $matches);

--
Richard Heyes
+44 (0)844 801 1072
http://www.websupportsolutions.co.uk

Knowledge Base and HelpDesk software
that can cut the cost of online support

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



Re: [PHP] preg_match_all to match img tags

2007-08-11 Thread Tijnema
On 8/11/07, Richard Heyes [EMAIL PROTECTED] wrote:
 Richard Heyes wrote:
  How can i match an image tag correctly so it does not cause any issues
  with how the user adds the image.
 
  preg_match_all('/img[^]*/Ui');
 
  Off the top of my head. This wouldn't allow for using the right angle
  bracket in the img tag, but that's almost never going to happen in reailty.

 Oops that should be:

 preg_match_all('/img[^]*/Ui', $input, $matches);

 --
 Richard Heyes

 img src=image.jpg

Your script doesn't catch above ;)

Tijnema
-- 
Vote for PHP Color Coding in Gmail! - http://gpcc.tijnema.info

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



Re: [PHP] preg_match_all to match img tags

2007-08-11 Thread Richard Heyes

   img src=image.jpg


Your script doesn't catch above ;)


So don't write HTML like that.

--
Richard Heyes
+44 (0)844 801 1072
http://www.websupportsolutions.co.uk

Knowledge Base and HelpDesk software
that can cut the cost of online support

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



Re: [PHP] preg_match_all to match img tags

2007-08-11 Thread Tijnema
On 8/11/07, Richard Heyes [EMAIL PROTECTED] wrote:
img src=image.jpg
 
  Your script doesn't catch above ;)

 So don't write HTML like that.

 Depends where the HTML is coming from, it might be user input

Tijnema
-- 
Vote for PHP Color Coding in Gmail! - http://gpcc.tijnema.info

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



Re: [PHP] preg_match_all to match img tags

2007-08-11 Thread Richard Heyes

   img src=image.jpg

Your script doesn't catch above ;)

So don't write HTML like that.


 Depends where the HTML is coming from, it might be user input


Ok, add \s* after the initial angle bracket.

--
Richard Heyes
+44 (0)844 801 1072
http://www.websupportsolutions.co.uk

Knowledge Base and HelpDesk software
that can cut the cost of online support

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



Re: [PHP] preg_match_all to match img tags

2007-08-11 Thread Stut

Tijnema wrote:

On 8/11/07, Richard Heyes [EMAIL PROTECTED] wrote:

Richard Heyes wrote:

How can i match an image tag correctly so it does not cause any issues
with how the user adds the image.

preg_match_all('/img[^]*/Ui');

Off the top of my head. This wouldn't allow for using the right angle
bracket in the img tag, but that's almost never going to happen in reailty.

Oops that should be:

preg_match_all('/img[^]*/Ui', $input, $matches);

--
Richard Heyes


 img src=image.jpg

Your script doesn't catch above ;)


That's not valid HTML and won't get displayed correctly in most browsers.

-Stut

--
http://stut.net/

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



Re: [PHP] preg_match_all to match img tags

2007-08-11 Thread Tijnema
On 8/11/07, Stut [EMAIL PROTECTED] wrote:
 Tijnema wrote:
  On 8/11/07, Richard Heyes [EMAIL PROTECTED] wrote:
  Richard Heyes wrote:
  How can i match an image tag correctly so it does not cause any issues
  with how the user adds the image.
  preg_match_all('/img[^]*/Ui');
 
  Off the top of my head. This wouldn't allow for using the right angle
  bracket in the img tag, but that's almost never going to happen in 
  reailty.
  Oops that should be:
 
  preg_match_all('/img[^]*/Ui', $input, $matches);
 
  --
  Richard Heyes
 
   img src=image.jpg
 
  Your script doesn't catch above ;)

 That's not valid HTML and won't get displayed correctly in most browsers.

 -Stut

hmm.. damn.. you're right :P I always assumed it was just correct...

Tijnema
-- 
Vote for PHP Color Coding in Gmail! - http://gpcc.tijnema.info

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



Re: [PHP] preg_match_all to match img tags

2007-08-11 Thread Ólafur Waage
Ive already finished the code and pasted it to this mailing list :)
Just an fyi.

Ólafur Waage

 2007/8/11, Tijnema [EMAIL PROTECTED]:
  On 8/11/07, Stut [EMAIL PROTECTED] wrote:
   Tijnema wrote:
On 8/11/07, Richard Heyes [EMAIL PROTECTED] wrote:
Richard Heyes wrote:
How can i match an image tag correctly so it does not cause any 
issues
with how the user adds the image.
preg_match_all('/img[^]*/Ui');
   
Off the top of my head. This wouldn't allow for using the right angle
bracket in the img tag, but that's almost never going to happen in 
reailty.
Oops that should be:
   
preg_match_all('/img[^]*/Ui', $input, $matches);
   
--
Richard Heyes
   
 img src=image.jpg
   
Your script doesn't catch above ;)
  
   That's not valid HTML and won't get displayed correctly in most browsers.
  
   -Stut
 
  hmm.. damn.. you're right :P I always assumed it was just correct...
 
  Tijnema
  --
  Vote for PHP Color Coding in Gmail! - http://gpcc.tijnema.info
 
  --
  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



Re: [PHP] preg_match_all to match img tags

2007-08-10 Thread Ólafur Waage
Guys i would like to thank you for the help.

Jan i used some of your code to help me get this. There was one issue
with yours, preg_match_all while in a foreach will only match the last
match and output that as the $m array.

Here is the code, i hope someone else can use it. Its a working code
in PHP4 and PHP5. I am currenly wrapping the imageManip(); around the
$_POST variable that is the blog post when im posting it to the SQL.
So the getimagesize(); only has to run once.

There is one bug, if the user resizes the image manually to something
less than 590, to say maybe 400, this program will still resize it to
590.

function imageProportions($image)
{
  $imageLoad = @getimagesize($image);

  // if the image is larger than 590 pixels, it will resize both
height and width
  // if not it will do nothing to the variables.
  if($imageLoad[0]  590)
  {
$proportion = $imageLoad[0] / $imageLoad[1];
$imageLoad[0] = 590;
$imageLoad[1] = $imageLoad[0] / $proportion;

return array($imageLoad[0], ceil($imageLoad[1]));
  }
  else
  {
return array($imageLoad[0], $imageLoad[1]);
  }
}

function imageManip($data)
{
  // match all image tags (thanks to Jan Reiter)
  @preg_match_all(/\ *[img][^\]*[.]*\/i, $data, $matches);

  if( is_array($matches[0]) )
  {
// put all those image tags in one string, since preg match all
needs the data
// to be in a string format
foreach($matches[0] as $match)
{
  $imageMatch .= $match;
}

// match all source links within the original preg match all output
@preg_match_all(/src=\(.+?)\/i, $imageMatch, $m);

// for each match that has the same key as the second match,
replace the entire
// tag with my img tag that includes width, height and border=0
foreach($matches[0] as $imageTagKey = $imageTag)
{
  foreach($m[1] as $imageSrcKey = $imageSrc)
  {
if($imageTagKey == $imageSrcKey)
{
  $imageStats = imageProportions($imageSrc);

  $data = str_replace($imageTag, img src=\.$imageSrc.\
width=\.$imageStats[0].\ height=\.$imageStats[1].\
border=\0\ /, $data);
}
  }
}
  }

  return $data;
}


2007/8/10, Jan Reiter [EMAIL PROTECTED]:
 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:[EMAIL PROTECTED]
 Sent: Friday, August 10, 2007 3:18 AM
 To: php-general@lists.php.net
 Subject: Re: [PHP] 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

Re: [PHP] preg_match_all to match img tags

2007-08-10 Thread Richard Lynch
On Thu, August 9, 2007 6:45 pm, Ó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.

Scaling the image in the browser is horrible for performance on the
client side...

The entire image still gets downloaded, and then the poor browser has
to scale this monster image down.

You may want to re-think your plan of attack...

You could, for example, force users to only use registered images,
and if they register an image large than 500, use http://php.net/gd
to scale it down.

As far as matching the image tag correctly goes, I'd have to suggest
that you try using a DOM to parse the HTML instead of regex.

That said, to get the WHOLE img tag in the same vein as you are using
now:
preg_match_all(/\ *[img][^\]*[src] *= *[\\']{0,1}([^\\'\ ]*)/i,

Note the addition of a closing  which will mark the end of the img
tag.

The [img] and [src] are kind of wonky, really, as they would also
match this bit of nonsense:

Sometimes i src=fooitalics/i could have bogus attributes.

YMMV

-- 
Some people have a gift link here.
Know what I want?
I want you to buy a CD from some indie artist.
http://cdbaby.com/browse/from/lynch
Yeah, I get a buck. So?

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



[PHP] preg_match_all to match img tags

2007-08-09 Thread Ólafur Waage
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.

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



Re: [PHP] preg_match_all to match img tags

2007-08-09 Thread brian

Ó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



RE: [PHP] preg_match_all to match img tags

2007-08-09 Thread Jan Reiter
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:[EMAIL PROTECTED] 
Sent: Friday, August 10, 2007 3:18 AM
To: php-general@lists.php.net
Subject: Re: [PHP] 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