|
Posted by Rik Wasmus on 10/29/07 21:38
On Mon, 29 Oct 2007 22:05:41 +0100, Jim <jimyt@yahoo.com> wrote:
> I just spotted one error, the equals sign after the negative
> lookbehind, although that seems to have made little difference.
> Updated code follows:
>
> <?php
>
> $prefix =3D 'http://mydomain.com/';
>
> $content =3D <<<EOT
> <img src=3D"images/test.gif" alt=3D"Test Image" />
> <img src=3D"http://shouldnt.be.changed/images/test.gif" alt=3D"Test
> Image" />
> EOT;
>
> echo preg_replace('/\<img(.+?)src=3D"(?<!http)(.+?)"(.+?)\/>/', '<img
> $1src=3D"' . $prefix . '$2"$3/>', $content);
>
> /*
> * First img element's src attribute should be
> * changed, but not the second's.
> */
>
> ?>
Well, taking a guess here, but 'http://' is not preceded by 'http', so i=
t =
will match in $2. Lookaheads/behinds without a literal match is often no=
t =
a very nice way to go. I'd use the preg_replace_callback or the /e =
modifier to let a function examine the match and act apropriately.
-- =
Rik Wasmus
[Back to original message]
|