You are here: Re: PCRE - Negative Lookbehind Assertion problem « PHP Programming Language « IT news, forums, messages
Re: PCRE - Negative Lookbehind Assertion problem

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

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация