Recent Topics

1 Nov 20, 2007 18:34    

Hey there,

I've startet to create a auto-thumbnail-script. Now I've got a problem with preg_replace:

$searcher =  '/<img(.*?)src="' . (addcslashes($match, './')) . '"(.*?)(width="([0-9].*)")?(.*?)(height="([0-9].*)")?(.*?)>/';

$replacer = '<img${1}src="' . $thumbfile . '"${2}${3}${5}>';

$content = preg_replace($searcher, $replacer, $content);

Ok, my Problem is to delete width and height attributes from the given image-tag and still keep other attributs like alt, title and border in the tag. It works if I delete all attributes and it works if I delete no attributes but I don't get it how to delete width and height AND only try to delete them when they are given.

I hope someone is good in regex... I like to publish this plugin to you as soon as possible :)

2 Nov 21, 2007 01:18

Try this:


preg_replace_callback(
  '~<img\s[^>]+>~',
  create_function( '$m', '
      return preg_replace( '~\b(width|height)=\S+~', '', $m[0] );' ),
  $content );

It's untested, but the idea is: find IMG tags and therein replace any width/height attribs with nothing.

3 Nov 21, 2007 02:56

Hey,
thanks but it doesn't work.

Here is my code:

$searcher =  '/<img(.*?)src="' . (addcslashes($match, './')) . '"(.*?)(width="([0-9]*)")*(.*?)(height="([0-9]*)")*(.*?)>/';
$replacer = '<img${1}src="' . $thumbfile . '"${2}${5}${8}>';

This works, but only if no height/width is given and if width is before height.

$searcher =  '/<img(.*?)src="' . (addcslashes($match, './')) . '"(.*?)(width="([0-9]*)")(.*?)(height="([0-9]*)")(.*?)>/';
$replacer = '<img${1}src="' . $thumbfile . '"${2}${5}${8}>';

This works to, but only if height/width is given and if width is before height.

The difference between this to codes is the * behind the height/width-areas. If someone can make this work in every way it would be very, very nice. Also it would be nice if height and width doesn't need to be next to each other...

4 Nov 21, 2007 20:33

Ok, got it :)


Form is loading...