SearchSearch   

str_replace question

 
   Webmaster Forums (Home) -> PHP RSS
Next:  Request: howto use a CLI php script at webserver ..  
Author Message
jmark

External


Since: Jul 12, 2007
Posts: 8



(Msg. 1) Posted: Sun Aug 12, 2007 12:45 pm
Post subject: str_replace question
Archived from groups: comp>lang>php (more info?)

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
Back to top
jmark

External


Since: Jul 12, 2007
Posts: 8



(Msg. 2) Posted: Sun Aug 12, 2007 3:25 pm
Post subject: Re: str_replace question
Archived from groups: per prev. post (more info?)

On Aug 12, 3:37 pm, Jerry Stuckle <jstuck....RemoveThis@attglobal.net> wrote:
> jm....RemoveThis@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....RemoveThis@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
Back to top
Jerry Stuckle

External


Since: Jul 08, 2004
Posts: 3787



(Msg. 3) Posted: Sun Aug 12, 2007 4:37 pm
Post subject: Re: str_replace question
Archived from groups: per prev. post (more info?)

jmark.RemoveThis@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.RemoveThis@attglobal.net
==================
Back to top
jmark

External


Since: Jul 12, 2007
Posts: 8



(Msg. 4) Posted: Mon Aug 13, 2007 8:04 am
Post subject: Re: str_replace question
Archived from groups: per prev. post (more info?)

On Aug 13, 2:44 am, gosha bine <stereof....DeleteThis@gmail.com> wrote:
> On 12.08.2007 21:45 jm....DeleteThis@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
Back to top
Toby A Inkster

External


Since: Jul 17, 2007
Posts: 111



(Msg. 5) Posted: Mon Aug 13, 2007 8:52 am
Post subject: Re: str_replace question
Archived from groups: per prev. post (more info?)

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/08/12/php-debugging-with-style/
Back to top
gosha bine

External


Since: Apr 27, 2007
Posts: 207



(Msg. 6) Posted: Mon Aug 13, 2007 9:44 am
Post subject: Re: str_replace question
Archived from groups: per prev. post (more info?)

On 12.08.2007 21:45 jmark.RemoveThis@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 Wink http://code.google.com/p/pihipi
Back to top
Display posts from previous:   
       Webmaster Forums (Home) -> PHP
Page 1 of 1

 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum