Re[4]: preg_replace: avoiding double replacements

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

 



Hello Peter,

Hm... I see I need to specify what I'm really doing. Actually, I need
to change the letters in the text. It's a famous and ancient crypting
method: you divide the alphabet making two parts, then you change the
letters of one part with letters for other part (so A becomes N, B
becomes O, etc., and vice versa). it works fine and slightly with
strtr or str_replace... but only if the text is not in utf-8 and it
doesn't contain any non-English letters such as Cyrillic what I need.
What my regex does is the following: it sees an A, well it changes it
to N; then it goes through the string and sees an N... what does it
do? Surely, it changes it back to A! I hoped (in vain) that there
exists a modifier preventing this behavior... but it seems that it's
false(
Thanks!
-- 
With best regards from Ukraine,
Andre
Skype: Francophile; Wlm&MSN: arthaelon @ yandex.ru; Jabber: arthaelon @ jabber.org
Yahoo! messenger: andre.polykanine; ICQ: 191749952
Twitter: m_elensule

----- Original message -----
From: Peter Lind <peter.e.lind@xxxxxxxxx>
To: Andre Polykanine <andre@xxxxxxxx>
Date: Tuesday, May 18, 2010, 10:19:51 AM
Subject:  preg_replace: avoiding double replacements

On 18 May 2010 09:04, Andre Polykanine <andre@xxxxxxxx> wrote:

[snip]

> Andre Polykanine wrote:
>> Hello everyone,
>>
>> Sorry for bothering you again.
>> Today I met a problem exactly described by a developer in users' notes
>> that follow the preg_replace description in the manual:
>> info at gratisrijden dot nl
>> 02-Oct-2009 02:48
>> if you are using the preg_replace with arrays, the replacements will apply as subject for the patterns later in the array. This means replaced values can
>> be replaced again.
>>
>> Example:
>> <?php
>> $text =
>> 'We want to replace BOLD with the <boldtag> and OLDTAG with the <newtag>';
>>
>> $patterns
>> = array(
>> '/BOLD/i',
>> '/OLDTAG/i');
>> $replacements
>> = array(
>> '<boldtag>',
>> '<newtag>');
>>
>> echo preg_replace
>> ($patterns, $replacements, $text);
>> ?>
>>
>> Output:
>> We want to replace <b<newtag>> with the <<b<newtag>>tag> and <newtag> with the <newtag>
>>
>> Look what happend with BOLD.
>>
>> Is there any solution to this besides any two-step sophisticated trick
>> like case changing?
>> Thanks!
>>

Use better regexes: either match for word endings or use a delimiter
in your markers (i.e. ###BOLD### instead of BOLD).

Regards
Peter

-- 
<hype>
WWW: http://plphp.dk / http://plind.dk
LinkedIn: http://www.linkedin.com/in/plind
Flickr: http://www.flickr.com/photos/fake51
BeWelcome: Fake51
Couchsurfing: Fake51
</hype>

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[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