joining str_replace

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Addos
Forum Contributor
Posts: 305
Joined: Mon Jan 17, 2005 4:13 pm

joining str_replace

Post by Addos »

Can I have the following joined as one complete string rather than having to pass the argument through str_replace twice?
Thanks

Code: Select all

<?php $myfile = 'r e move space s';
 
 $fileName1 = str_replace('-' , '_' ,$myfile);
 $fileName2 = str_replace(' ' , '_' ,$fileName1);
 
echo $fileName2; ?>
Last edited by Addos on Fri Feb 29, 2008 11:26 am, edited 1 time in total.
lepad
Forum Newbie
Posts: 12
Joined: Fri Oct 05, 2007 9:58 am

Re: joining str_replace

Post by lepad »

Code: Select all

 
$myfile = 'r e move space s';
$new_string = str_replace(array('-' , ' '), array('_', '_'), $myfile);
echo $new_string;
 
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: joining str_replace

Post by Jonah Bron »

or

Code: Select all

<?php
$myfile = 'r e move space s';
$myfile = str_replace('-' , '_' , str_replace(' ', '_', $myfile));
echo $myfile;
?>
Note: always use <?php, not <?
Addos
Forum Contributor
Posts: 305
Joined: Mon Jan 17, 2005 4:13 pm

Re: joining str_replace

Post by Addos »

Thank you.
Both are perfect for me.
Post Reply