473,387 Members | 1,516 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

Aug 12 '07 #1
5 1675
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.
js*******@attglobal.net
==================
Aug 12 '07 #2
On Aug 12, 3:37 pm, Jerry Stuckle <jstuck...@attglobal.netwrote:
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
Aug 12 '07 #3
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
Aug 13 '07 #4
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/
Aug 13 '07 #5
On Aug 13, 2:44 am, gosha bine <stereof...@gmail.comwrote:
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

Aug 13 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Jon | last post by:
Why does the following not replace the ":)"? str_replace(":)", "&nbsp;<img src=http://www.com.com/forum/emoticons/smiley.gif>&nbsp;", $_POST); But, the following does replace the ":("? ...
9
by: Hugo Coolens | last post by:
I need to use the str_replace function for a php-script which works together with html/javascript. In the script I have to perform things like: $arabic=str_replace ("'","\'", $row);...
1
by: marcomputers | last post by:
Hi, I'm trying to automatically update an existing rss podcast file using php and the str_replace function. Every time I add a media file, it should read the .rss file into a string, take the...
9
by: Wayne | last post by:
$a = $_POST; # txt_content = This is a<CR><LF>Test $p = str_replace ("%0D%0A", "<br>", $a); That is the above code that I am using, however, it is not picking up the CR/LF from the textarea. I...
4
by: The Numerator | last post by:
I have this page (print.php). In that file, I want to to include a text file from "songs/breathe.txt", and replace all instances of "<b" with "<span" and "</b>" with "</span>". I know how I should...
2
by: kencana | last post by:
Hi all, I got a doubt about the str_replace function. <?php $string="avenue 1, ave 1"; $words=str_replace ("ave","avenue", $string); echo $words; ?>
4
by: d1srupt3r | last post by:
I am developing my form into a captcha secured form and I used header() function if the user didn't put the verification code properly and still when the user go back to the form page the filled he...
3
by: Confused but working on it | last post by:
So I've been trying to get a bit of code to: Read all of the files in a dir called thumbs, but not the . and .., use the filename in a link to get the same filename in an images dir. Now I'm...
1
by: Sean Kim | last post by:
From example in the book 'Programming PHP' (O'Reilly) (p.305) $theKey = 'DESTINATION'; $target = '{' . $theKey . '}'; $inValues = 'some.php'; //$theTemplate = str_replace("\{$theKey}",...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.