Re[2]: [PHP] preg_replace: avoiding double replacements

2010-05-18 Thread Andre Polykanine
Hello Jim,

That might work for that particular example, but I have utf-8 strings
containing different characters of different alphabets, so neither
str_replace nor strtr work...
Thanks!
-- 
With best regards from Ukraine,
Andre
Skype: Francophile; WlmMSN: arthaelon @ yandex.ru; Jabber: arthaelon @ 
jabber.org
Yahoo! messenger: andre.polykanine; ICQ: 191749952
Twitter: m_elensule

- Original message -
From: Jim Lucas li...@cmsws.com
To: Andre Polykanine an...@oire.org
Date: Tuesday, May 18, 2010, 3:33:09 AM
Subject: [PHP] preg_replace: avoiding double replacements

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 bnewtag with the bnewtagtag 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!
 
 --
 With best regards from Ukraine,
 Andre
 Http://oire.org/ - The Fantasy blogs of Oire
 Skype: Francophile; WlmMSN: arthaelon @ yandex.ru; Jabber: arthaelon @ 
 jabber.org
 Yahoo! messenger: andre.polykanine; ICQ: 191749952
 Twitter: http://twitter.com/m_elensule
 
 

Well, for the example you gave, why use regex?  Check this out as an example.

plaintext?php
$text = 'We want to replace BOLD with the boldtag and OLDTAG with the 
newtag';

$regex = array(
'/BOLD/i',
'/OLDTAG/i',
);
$oldtags = array(
'BOLD',
'OLDTAG',
);
$replacements = array(
'boldtag',
'newtag',
);

# Original String
echo $text.\n;

# After regex is applied
echo preg_replace($regex, $replacements, $text).\n;

# After plain tag replacement happens
echo str_replace($oldtags, $replacements, $text).\n;

?

See if that works for you.

-- 
Jim Lucas

   Some men are born to greatness, some achieve greatness,
   and some have greatness thrust upon them.

Twelfth Night, Act II, Scene V
by William Shakespeare

-- 
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



Re: Re[2]: [PHP] preg_replace: avoiding double replacements

2010-05-18 Thread Peter Lind
On 18 May 2010 09:04, Andre Polykanine an...@oire.org 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 bnewtag with the bnewtagtag 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



Re[2]: [PHP] preg_replace();

2007-02-02 Thread wwww
I have tasted the code and it worked fine (if I got you right):

$old_string=lazy \|\ dog;
$new_string=str_replace('|', '_', $old_string);
print $new_string;

I got lazy_dog

Ed

Friday, February 2, 2007, 10:01:14 PM, you wrote:

 Thanks,

 but I think that I must use preg_replace because the condition is: replace
 the chars (pipe or space) when they are between 

 ie :  src=file:///h|/hjcjdgh dlkgj/dgjk.jpg  to 
 src=file:///h_/hjcjdgh_dlkgj/dgjk.jpg

 Seb





 - Original Message - 
 From: [EMAIL PROTECTED]
 To: Sébastien WENSKE [EMAIL PROTECTED]
 Cc: php-general@lists.php.net
 Sent: Friday, February 02, 2007 8:38 PM
 Subject: Re: [PHP] preg_replace();


 I am not a very experienced programmer, but I think that str_replace
 can be used in this case:
 $new_string=str_replace('|', '_', $old_string)

 then use the same function to replace spaces.

 Ed

 Friday, February 2, 2007, 9:30:37 PM, you wrote:

 Hi all,

 I want replace the | (pipe) and the   (space) chars where are
 between  (double-quotes)  by an underscore _ with the
 preg_replace(); funtction.

 Can someone help me to find the correct regex.

 Thanks in advance

 Seb

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