Page 1 of 1

joining str_replace

Posted: Fri Feb 29, 2008 10:44 am
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; ?>

Re: joining str_replace

Posted: Fri Feb 29, 2008 10:47 am
by lepad

Code: Select all

 
$myfile = 'r e move space s';
$new_string = str_replace(array('-' , ' '), array('_', '_'), $myfile);
echo $new_string;
 

Re: joining str_replace

Posted: Fri Feb 29, 2008 11:07 am
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 <?

Re: joining str_replace

Posted: Fri Feb 29, 2008 11:24 am
by Addos
Thank you.
Both are perfect for me.