|
Posted by Jim on 10/29/07 20:49
Hi,
I'm trying to prefix the "src" attribute of all "img" elements with a
given string, $prefix. Here's what I've got:
preg_replace('/\<img(.+?)src="(?<!=http)(.+?)"(.+?)\/>/', '<img
$1src="' . $prefix . '$2"$3/>', $content);
The problem comes in that it always performs the replace, even when
there's an "http" in the source attribute.
Here's the source for a quick test if anyone's interested:
<?php
$prefix = 'http://mydomain.com/';
$content = <<<EOT
<img src="images/test.gif" alt="Test Image" />
<img src="http://shouldnt.be.changed/images/test.gif" alt="Test
Image" />
EOT;
echo preg_replace('/\<img(.+?)src="(?<!=http)(.+?)"(.+?)\/>/', '<img
$1src="' . $prefix . '$2"$3/>', $content);
/*
* First img element's src attribute should be changed, but not the
second's.
*/
?>
Thanks to all,
Jim.
Navigation:
[Reply to this message]
|