LiamProductions Posted July 10, 2007 Share Posted July 10, 2007 Hi, What would str_replaced be used for? please explain. Quote Link to comment https://forums.phpfreaks.com/topic/59303-solved-str_replace-function/ Share on other sites More sharing options...
OLG Posted July 10, 2007 Share Posted July 10, 2007 str_replace — Replace all occurrences of the search string with the replacement string $string = "lets replace me"; str_replace("replace", "", $string); str_replace(SearchTerm, ReplacementTerm, Subject) If subject is an array, then the search and replace is performed with every entry of subject, and the return value is an array as well. Will produce "lets me" What a fun little example Quote Link to comment https://forums.phpfreaks.com/topic/59303-solved-str_replace-function/#findComment-294575 Share on other sites More sharing options...
LiamProductions Posted July 10, 2007 Author Share Posted July 10, 2007 So whatever is in the replacement turn gets changed to what is in the replacement term? so because you put "" it took out replace so it says lets me, but if you put: $string = "lets replace me"; str_replace("replace", "kiss", $string); If you put that code, would it say "Lets kiss me" ? Quote Link to comment https://forums.phpfreaks.com/topic/59303-solved-str_replace-function/#findComment-294581 Share on other sites More sharing options...
chocopi Posted July 10, 2007 Share Posted July 10, 2007 A good example is if you are saving files and want to replace any of the spaces with underscores <?php $str = "hello world.gif"; $new_str = str_replace(" ","_",$str); echo $str; // should give hello world.gif echo $new_str; // should give hello_world.gif ?> Quote Link to comment https://forums.phpfreaks.com/topic/59303-solved-str_replace-function/#findComment-294583 Share on other sites More sharing options...
LiamProductions Posted July 10, 2007 Author Share Posted July 10, 2007 Oh ok. Helps a little. Quote Link to comment https://forums.phpfreaks.com/topic/59303-solved-str_replace-function/#findComment-294586 Share on other sites More sharing options...
OLG Posted July 10, 2007 Share Posted July 10, 2007 Yes, that example is correct Liam Quote Link to comment https://forums.phpfreaks.com/topic/59303-solved-str_replace-function/#findComment-294587 Share on other sites More sharing options...
LiamProductions Posted July 10, 2007 Author Share Posted July 10, 2007 Yes, that example is correct Liam Mine or the latest example? Quote Link to comment https://forums.phpfreaks.com/topic/59303-solved-str_replace-function/#findComment-294588 Share on other sites More sharing options...
OLG Posted July 10, 2007 Share Posted July 10, 2007 Yours...... Quote Link to comment https://forums.phpfreaks.com/topic/59303-solved-str_replace-function/#findComment-294589 Share on other sites More sharing options...
per1os Posted July 10, 2007 Share Posted July 10, 2007 <?php $template = "<html><head><TITLE>%TITLE%</title></head><body>Welcome to <b>%TITLE%</b> Your name is %USER_NAME% we welcome you %USER_NAME%</body></html>"; $replace_array = array("%TITLE%" => "My Homepage", "%USER_NAME%" => "Jack of All Trades"); foreach ($replace_array as $replace => $what) { $template = str_replace($replace,$what,$template); } echo $template; ?> Makes it so you can store templates in databases easy without having to worry about using eval and being exploited etc. Quote Link to comment https://forums.phpfreaks.com/topic/59303-solved-str_replace-function/#findComment-294590 Share on other sites More sharing options...
LiamProductions Posted July 10, 2007 Author Share Posted July 10, 2007 Thanks for the help Quote Link to comment https://forums.phpfreaks.com/topic/59303-solved-str_replace-function/#findComment-294596 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.