str_replace question

str_replace question

am 12.08.2007 21:45:54 von jmark

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

Re: str_replace question

am 12.08.2007 22:37:20 von Jerry Stuckle

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

Re: str_replace question

am 13.08.2007 00:25:17 von jmark

On Aug 12, 3:37 pm, Jerry Stuckle 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

Re: str_replace question

am 13.08.2007 09:44:32 von gosha bine

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

Re: str_replace question

am 13.08.2007 09:52:33 von Toby A Inkster

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/

Re: str_replace question

am 13.08.2007 17:04:39 von jmark

On Aug 13, 2:44 am, gosha bine 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