al3x8730 Posted September 2, 2008 Share Posted September 2, 2008 I'm trying to replace more than one thing, but if I repeat the function echo str_replace() then it doesn't work because it repeats other things. how would I replace more than one thing? Quote Link to comment https://forums.phpfreaks.com/topic/122406-question-reguarding-echo-str_replace/ Share on other sites More sharing options...
cooldude832 Posted September 2, 2008 Share Posted September 2, 2008 Like <?php $string = "Joe goes to school"; $string = str_replace("Joe","Sally",$string); $string = str_replace("school", "mall",$string); echo $string; #Sally goes to the mall ?> Quote Link to comment https://forums.phpfreaks.com/topic/122406-question-reguarding-echo-str_replace/#findComment-632048 Share on other sites More sharing options...
Ken2k7 Posted September 2, 2008 Share Posted September 2, 2008 Do you want to replace one thing multiple times, multiple things one time or multiple things multiple times? Quote Link to comment https://forums.phpfreaks.com/topic/122406-question-reguarding-echo-str_replace/#findComment-632049 Share on other sites More sharing options...
al3x8730 Posted September 2, 2008 Author Share Posted September 2, 2008 Actually I'm trying to replace something in a directory contents listing. I want to replace 3 file names. And for 1 I'm using this: <?php $handle=opendir("."); while (($file = readdir($handle))!==false) { echo str_replace( "index.php", "", $file ) . " <br />"; } closedir($handle); ?> But if I repeat the line: echo str_replace( "index.php", "", $file ) . " <br />"; and just replace it with a different file name It doesn't work because it repeats the other files names. Quote Link to comment https://forums.phpfreaks.com/topic/122406-question-reguarding-echo-str_replace/#findComment-632055 Share on other sites More sharing options...
Ken2k7 Posted September 2, 2008 Share Posted September 2, 2008 Why didn't you use the rename() function? Quote Link to comment https://forums.phpfreaks.com/topic/122406-question-reguarding-echo-str_replace/#findComment-632059 Share on other sites More sharing options...
The Little Guy Posted September 2, 2008 Share Posted September 2, 2008 $find = array('Joe','school'); $replace = array('Bob','work'); $string = "Joe goes to school"; $new = str_replace($find,$replace,$string); echo $new; Quote Link to comment https://forums.phpfreaks.com/topic/122406-question-reguarding-echo-str_replace/#findComment-632061 Share on other sites More sharing options...
al3x8730 Posted September 2, 2008 Author Share Posted September 2, 2008 Why didn't you use the rename() function? I don't want to rename the file, just rename it on that list. Quote Link to comment https://forums.phpfreaks.com/topic/122406-question-reguarding-echo-str_replace/#findComment-632063 Share on other sites More sharing options...
wildteen88 Posted September 2, 2008 Share Posted September 2, 2008 Why didn't you use the rename() function? I don't want to rename the file, just rename it on that list. I presume you mean you don't want the index.php file displayed in the generated list. You should do this instead: <?php $handle = opendir("."); $ignore_files = array('.', '..', 'index.php'); while (($file = readdir($handle))!==false) { // do not display files listed in the ignore_files array if(!in_array($file, $ignore_files)) { echo $file . '<br />'; } } closedir($handle); ?> Quote Link to comment https://forums.phpfreaks.com/topic/122406-question-reguarding-echo-str_replace/#findComment-632107 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.