Hi folks, l’m trying to work with a little preg replace script to chagne 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?