Re: str_replace weird output

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

 



On Thursday 02 June 2005 09:52, Johan.Barbier@xxxxxxxxxxx wrote:
> But if I do that :
>
> <?php
> $texte = 'cd' ;
> $original = array('a', 'b', 'c', 'd', 'e', 'f', 'g');
> $modif = array ('c', 'd', 'e', 'f', 'g', 'h', 'i');
> $texte = str_replace($original, $modif, $texte) ;
> echo $texte, ' <br />' ;
> ?>
>
> The result is : ih
>
> Why ?

You should know that, unless you tell php to limit the number of replaces, it 
will keep on replacing until it doesn't find a match anymore. 

Here is what happens:

 <?php
 $texte = 'cd' ;
 $original = array('a', 'b', 'c', 'd', 'e', 'f', 'g');
 $modif = array ('c', 'd', 'e', 'f', 'g', 'h', 'i');
 $texte = str_replace($original, $modif, $texte) ;
#after first replacement
$texte='ef'
#after 2nd replacement
$texte='gh'
#after third replacement
$texte='ih';

If you want to prevent this, tell the function that you only want 2 
replacements.  Like this:
$limite=2;
$texte=str_replace($original,$modif,$texte,$limite);

Hope this helps

With kind regards


ps: the php documentation is also available in French.  Check out: 
http://fr2.php.net/manual/fr/function.str-replace.php for more info on 
str_replace

Andy

-- 
Registered Linux User Number 379093
-- --BEGIN GEEK CODE BLOCK-----
Version: 3.1
GAT/O/>E$ d-(---)>+ s:(+)>: a--(-)>? C++++$(+++) UL++++>++++$ P-(+)>++
L+++>++++$ E---(-)@ W+++>+++$ !N@ o? !K? W--(---) !O !M- V-- PS++(+++)
PE--(-) Y+ PGP++(+++) t+(++) 5-- X++ R*(+)@ !tv b-() DI(+) D+(+++) G(+)
e>++++$@ h++(*) r-->++ y--()>++++
-- ---END GEEK CODE BLOCK------
--
Check out these few php utilities that I released
 under the GPL2 and that are meant for use with a 
 php cli binary:
 
 http://www.vlaamse-kern.com/sas/
--

--

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