...

View Full Version : preg_replace not replacing all instances...what am l missing?



Coastal Web
03-31-2010, 02:05 AM
Hi folks, l'm trying to work with a little preg replace script to change the SRC of images posted on one of my wordpress blogs. Basically what l'm trying to do is have the preg_match() function strip out the width and height attributes from the image(s) and alter the SRC attribute to point to a timthumb.php resizing script.




$content = '<img src="http://blogs.theworldlink.com/photoblog/wp-content/uploads/2010/03/pics-13.jpg" alt="" title="pics-(13)" width="820" height="668" class="alignnone size-full wp-image-64" />';

$content .= '<img src="http://blogs.theworldlink.com/photoblog/wp-content/uploads/2010/03/pics-12.jpg" alt="" title="pics-(12)" width="820" height="757" class="alignnone size-full wp-image-63" />';


$content_fixed = preg_replace('/<img(.*)src=(\'|")(.*)(\'|")(.*)width=(\'|")(.*)(\'|")(.*)height=(\'|")(.*)(\'|")(.*)>/ims', "<img$1src=\"/photoblog/wp-content/themes/photoblog/scripts/timthumb.php?zc=0&w=580&src=$3$11\" /><br />", $content);


echo $content . '<br />----------------<br />' . $content_fixed;


Outputs:


<img src="http://blogs.theworldlink.com/photoblog/wp-content/uploads/2010/03/pics-13.jpg" alt="" title="pics-(13)" width="820" height="668" class="alignnone size-full wp-image-64" />

<img src="http://blogs.theworldlink.com/photoblog/wp-content/uploads/2010/03/pics-12.jpg" alt="" title="pics-(12)" width="820" height="757" class="alignnone size-full wp-image-63" />

----------------

<img src="http://blogs.theworldlink.com/photoblog/wp-content/uploads/2010/03/pics-13.jpg" alt="" title="pics-(13)" width="820" height="668" class="alignnone size-full wp-image-64" />

<img src="/photoblog/wp-content/themes/photoblog/scripts/timthumb.php?zc=0&w=580&src=http://blogs.theworldlink.com/photoblog/wp-content/uploads/2010/03/pics-12.jpg" alt="" title="pics-(12)757" class="alignnone size-full wp-image-63" /><br />


For some reason it's only applying to the last IMG tag, and l'm not sure why. Can anyone that's a little more seasoned with preg_replace help me out a bit and tell me what l'm doing wrong, and how l can fix it?

DJCMBear
03-31-2010, 02:15 AM
You say preg_match but your using preg_replace??

And use preg_match_all(); or preg_replace_all();

MattF
03-31-2010, 02:20 AM
There is no preg_replace_all function. I'd say making the expression not greedy might help:



These:

(.*)

ought be:

(.*?)

Coastal Web
03-31-2010, 02:20 AM
Ooops, sorry yeah I meant preg replace, my mind was thinking about the end of the work day I guess. I don't think there is a preg_replace_all, preg_replace is supposed to be greedy by default.

MattF
03-31-2010, 02:22 AM
preg_replace is supposed to be greedy by default.

Which is most likely the cause of your problem.

Coastal Web
03-31-2010, 02:24 AM
Mattf, greedy as in its supposed to change all found instances unless a 4th varfable is given.

MattF
03-31-2010, 02:27 AM
Mattf, greedy as in its supposed to change all found instances unless a 4th varfable is given.

That isn't what greedy does or means. It will go to the last match it finds instead of stopping at each specific instance. Just do as I suggested above. You can argue the toss on semantics if that doesn't work.

DJCMBear
03-31-2010, 02:46 AM
oh my bad no preg_replace_all i was thinking about preg_replace_callback sorry

Coastal Web
03-31-2010, 04:40 AM
That isn't what greedy does or means. It will go to the last match it finds instead of stopping at each specific instance. Just do as I suggested above. You can argue the toss on semantics if that doesn't work.

Hi MattF,

Thanks for the clarification, you are correct l mis-understood what greedy meant. I've tried your solution of changing the (.*)'s to (.*?) doesn't seem to solve the problem but outputs things differently.

I'm currently using:
$content = preg_replace('/<img(.*?)src=(\'|")(.*?)(\'|")(.*?)>/ims', "<img$1src=\"/photoblog/wp-content/themes/photoblog/scripts/timthumb.php?zc=0&w=580&src=$3\" alt=\"\"><br />", get_the_content());

Which basically just strips everything after the src attribute and seems to work; I'd rather have it remove the height/width attributes and keep the rest, but l just can't seem to get it to work.

Coastal Web
03-31-2010, 04:44 AM
I found this little snippet and am using it after the fact to remove the height/width attributes from all the images:

$content = preg_replace("!(<img)([^<>]+)(/>)!e","'\\1'.preg_replace('!(height|width)=\"[^\"]*\"!i','',\"\\2\").'\\3'", $content);


I'm not nearly good enough at regular expressions to understand what's going on, but it seems to do the trick.

Thanks to all that have helped.

MattF
03-31-2010, 05:25 AM
Give that a whirl. It's untested, but should give you what you're after, I believe, (provided the format is consistent). $1 and $2 are the bits you want.



'#<img (.+?)(?:width="[^"]+" height="[^"]+")([^\>]+?)/>#ims'


That'll grab everything except for the height and width tags.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum