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
Addos
Forum Contributor
Posts: 305 Joined: Mon Jan 17, 2005 4:13 pm
Post
by Addos » Fri Feb 29, 2008 10:44 am
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
Post
by lepad » Fri Feb 29, 2008 10:47 am
Code: Select all
$myfile = 'r e move space s';
$new_string = str_replace(array('-' , ' '), array('_', '_'), $myfile);
echo $new_string;
Jonah Bron
DevNet Master
Posts: 2764 Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California
Post
by Jonah Bron » Fri Feb 29, 2008 11:07 am
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
Post
by Addos » Fri Feb 29, 2008 11:24 am
Thank you.
Both are perfect for me.