Re: One more time about regexes

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Wed, May 26, 2010 at 11:10 PM, Nilesh Govindarajan <lists@xxxxxxxxxx>wrote:

> ---------- Forwarded message ----------
> From: Nilesh Govindarajan <lists@xxxxxxxxxx>
> Date: Thu, May 27, 2010 at 8:40 AM
> Subject: Re:  One more time about regexes
> To: Andre Polykanine <andre@xxxxxxxx>
>
>
> On Thu, May 27, 2010 at 3:33 AM, Andre Polykanine <andre@xxxxxxxx> wrote:
> > Hello everyone,
> >
> > Sorry, but I'm asking one more time (since it's really annoying me and
> > I need to apply some really dirty hacks):
> > Is there a way making preg_replace() pass through the regex one single
> > time searching from left to right and not to erase what it has already
> > done?
> > I can give you a real task I'm accomplishing but the tasks requiring
> > that tend to multiply...
> > Thanks a lot!
> >
> > --
> > With best regards from Ukraine,
> > Andre
> > Http://oire.org/ - The Fantasy blogs of Oire
> > Skype: Francophile; Wlm&MSN: arthaelon @ yandex.ru; Jabber: arthaelon @
> jabber.org
> > Yahoo! messenger: andre.polykanine; ICQ: 191749952
> > Twitter: http://twitter.com/m_elensule
> >
> >
> > --
> > PHP General Mailing List (http://www.php.net/)
> > To unsubscribe, visit: http://www.php.net/unsub.php
> >
> >
>
>
> If you don't want to replace the stuff it has searched then use preg_match
> !
>
> --
> Nilesh Govindarajan
> Facebook: nilesh.gr
> Twitter: nileshgr
> Website: www.itech7.com
>
>
>
> --
> Nilesh Govindarajan
> Facebook: nilesh.gr
> Twitter: nileshgr
> Website: www.itech7.com
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
If I'm understanding correctly, Andre, you want to perform a replace
operation, but you're observing that there's an element of recursion
happening (e.g., you replace one item with its replacement, and then the new
replacement is also replaced.)

To my knowledge, this won't happen with a standard preg_replace():

$string = 'Bil & Tom & Phil';
$pattern = '/&/';
$replacement = '&amp;';
// outputs Bil &amp; Tom &amp; Phil
echo preg_replace($pattern, $replacement, $string);

However, if you're using arrays to pass in the patterns and replacements,
then one array item could later be replaced by another (see comment and
example on this page by info at gratisrijden dot nl):
http://php.net/manual/en/function.preg-replace.php

If that's the case, you have to carefully structure the order of the array
items so-as to preclude one replacement having the opportunity to override a
subsequent replacement.

Sorry if I misunderstood.  You might want to create a simple example of code
showing what you're working through.

Adam


-- 
Nephtali:  PHP web framework that functions beautifully
http://nephtaliproject.com

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux