473,761 Members | 9,379 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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($le tters, $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 1693
jm***@fastermai l.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($le tters, $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*******@attgl obal.net
=============== ===
Aug 12 '07 #2
On Aug 12, 3:37 pm, Jerry Stuckle <jstuck...@attg lobal.netwrote:
jm...@fastermai l.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($le tters, $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...@attgl obal.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***@fastermai l.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($le tters, $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...@gma il.comwrote:
On 12.08.2007 21:45 jm...@fastermai l.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($le tters, $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
4608
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 ":("? str_replace(":(", "&nbsp;<img src=http://www.com.com/forum/emoticons/crying.gif>&nbsp;", $_POST);
9
21547
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); $arabic=str_replace ("sh", "\š", $row); the second line works perfectly, however I can't make the first one work, it should replace a single quote with a quote preceded by a backslash. I presume I have to escape some characters to make this happen however...
1
1678
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 closing </channel> and </rss> tags out of it, and add the new rss file info to the end of the existing string, writing it back to the rss file. However, str_replace will not replace the </channel> and </rss> tags with ' '. I'm new to php, any help...
9
43159
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 have also attempted singular variations of the CR/LF combination and even reversed the sequence without success. Is it possible that it may be encoded differently and if so what is it? Thanks
4
4576
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 use str_replace and I tried to do this: <?php $replace_a=str_replace("<span","<b",include("text/breathe.txt")); $replace_b=str_replace("</span>",</b>",$replace_a);
2
1124
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
3072
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 have filled have still remained. In addition, users are usually submitting multiliner texts in the textbox so I used the str_replace() function to make all \n into %n% to prevent an error in the header function whenever the form is submitted. my...
3
3445
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 trying to use a foreach and glob as suggested, and get rid of the part of the string that has the dir info. SO the below produces all my thumbs nicely and all are links. <?php foreach (glob("thumbs/*.jpg") as $file) {
1
1260
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}", $inValues, theTemplate); //$theTemplate = str_replace("{$theKey}", $inValues, theTemplate);
0
9336
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9948
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9902
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8770
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7327
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6603
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5215
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
3446
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2738
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.