str_replace question

This is a discussion on str_replace question within the PHP Language forums, part of the PHP Programming Forums category; I saw this example in php.net // Outputs: apearpearle pear $letters = array('a', 'p'); $fruit = array('apple', 'pear'); $text = 'a ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 08-12-2007
jmark@fastermail.com
 
Posts: n/a
Default str_replace question

I saw this example in php.net

// Outputs: apearpearle pear
$letters = array('a', 'p');
$fruit = array('apple', 'pear');
$text = 'a p';
$output = str_replace($letters, $fruit, $text);
echo $output;

and I am bit puzzled why the output is the way it is instead of

apple pear

it looks like str_replace is replacing the p in apple to pear but why
does not replace the p in pear to pear and the a to apple and why is
the output not "apple pear"?

Thanks
John

Reply With Quote
  #2 (permalink)  
Old 08-12-2007
Jerry Stuckle
 
Posts: n/a
Default Re: str_replace question

jmark@fastermail.com wrote:
> I saw this example in php.net
>
> // Outputs: apearpearle pear
> $letters = array('a', 'p');
> $fruit = array('apple', 'pear');
> $text = 'a p';
> $output = str_replace($letters, $fruit, $text);
> echo $output;
>
> and I am bit puzzled why the output is the way it is instead of
>
> apple pear
>
> it looks like str_replace is replacing the p in apple to pear but why
> does not replace the p in pear to pear and the a to apple and why is
> the output not "apple pear"?
>
> Thanks
> John
>


John,

I haven't looked at the source code, but I would expect it does would be
effectively the same as:

$output = str_replace('a', 'apple', 'a p');
$output = str_replace('p', 'pear', '$output);

Which produces the same output.



--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Reply With Quote
  #3 (permalink)  
Old 08-13-2007
jmark@fastermail.com
 
Posts: n/a
Default Re: str_replace question

On Aug 12, 3:37 pm, Jerry Stuckle <jstuck...@attglobal.net> wrote:
> jm...@fastermail.com wrote:
> > I saw this example in php.net

>
> > // Outputs: apearpearle pear
> > $letters = array('a', 'p');
> > $fruit = array('apple', 'pear');
> > $text = 'a p';
> > $output = str_replace($letters, $fruit, $text);
> > echo $output;

>
> > and I am bit puzzled why the output is the way it is instead of

>
> > apple pear

>
> > it looks like str_replace is replacing the p in apple to pear but why
> > does not replace the p in pear to pear and the a to apple and why is
> > the output not "apple pear"?

>
> > Thanks
> > John

>
> John,
>
> I haven't looked at the source code, but I would expect it does would be
> effectively the same as:
>
> $output = str_replace('a', 'apple', 'a p');
> $output = str_replace('p', 'pear', '$output);
>
> Which produces the same output.
>
> --
> ==================
> Remove the "x" from my email address
> Jerry Stuckle
> JDS Computer Training Corp.
> jstuck...@attglobal.net
> ==================- Hide quoted text -
>
> - Show quoted text -


Yes, that sounds reasonable as to how it is working.
I had thought that str_replace replaces all elements in the array at
the same time. I think this would have been a more intuitive approach

Thanks
John


Reply With Quote
  #4 (permalink)  
Old 08-13-2007
gosha bine
 
Posts: n/a
Default Re: str_replace question

On 12.08.2007 21:45 jmark@fastermail.com wrote:
> I saw this example in php.net
>
> // Outputs: apearpearle pear
> $letters = array('a', 'p');
> $fruit = array('apple', 'pear');
> $text = 'a p';
> $output = str_replace($letters, $fruit, $text);
> echo $output;
>
> and I am bit puzzled why the output is the way it is instead of
>
> apple pear
>
> it looks like str_replace is replacing the p in apple to pear but why
> does not replace the p in pear to pear and the a to apple and why is
> the output not "apple pear"?
>


what you probably want is strtr

$repl = array('a' => 'apple', 'p' => 'pear');
$text = 'a p';
echo strtr($text, $repl);



--
gosha bine

makrell ~ http://www.tagarga.com/blok/makrell
php done right ;) http://code.google.com/p/pihipi
Reply With Quote
  #5 (permalink)  
Old 08-13-2007
Toby A Inkster
 
Posts: n/a
Default Re: str_replace question

jmark wrote:

> I had thought that str_replace replaces all elements in the array at
> the same time. I think this would have been a more intuitive approach


That would be the most sensible thing to do, but PHP does not always do
the most sensible thing!

--
Toby A Inkster BSc (Hons) ARCS
[Geek of HTML/SQL/Perl/PHP/Python/Apache/Linux]
[OS: Linux 2.6.12-12mdksmp, up 53 days, 11:31.]

PHP Debugging with Style -OR- How I Learned to Stop Worrying and Love the Bug
http://tobyinkster.co.uk/blog/2007/0...ng-with-style/
Reply With Quote
  #6 (permalink)  
Old 08-13-2007
jmark@fastermail.com
 
Posts: n/a
Default Re: str_replace question

On Aug 13, 2:44 am, gosha bine <stereof...@gmail.com> wrote:
> On 12.08.2007 21:45 jm...@fastermail.com wrote:
>
>
>
>
>
> > I saw this example in php.net

>
> > // Outputs: apearpearle pear
> > $letters = array('a', 'p');
> > $fruit = array('apple', 'pear');
> > $text = 'a p';
> > $output = str_replace($letters, $fruit, $text);
> > echo $output;

>
> > and I am bit puzzled why the output is the way it is instead of

>
> > apple pear

>
> > it looks like str_replace is replacing the p in apple to pear but why
> > does not replace the p in pear to pear and the a to apple and why is
> > the output not "apple pear"?

>
> what you probably want is strtr
>
> $repl = array('a' => 'apple', 'p' => 'pear');
> $text = 'a p';
> echo strtr($text, $repl);
>
> --
> gosha bine
>
> makrell ~http://www.tagarga.com/blok/makrell
> php done right ;)http://code.google.com/p/pihipi- Hide quoted text -
>
> - Show quoted text -


Thanks for showing me this function. I like it

Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 09:34 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0