dreamwest Posted November 20, 2008 Share Posted November 20, 2008 i want to use a config.php file and string replace the variables within another.php file. but im stuck with the str_replace config.php: <?php $customize = "this is the message"; str_replace("{customize}",$customize); ?> another.php: <html> <head> </head> <body> <? include_once ('/config.php');?> {customize} </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/133450-how-to-str_replace/ Share on other sites More sharing options...
teng84 Posted November 20, 2008 Share Posted November 20, 2008 str_replace needs atleast 3 parameters search key words /string replacement / the string Quote Link to comment https://forums.phpfreaks.com/topic/133450-how-to-str_replace/#findComment-694107 Share on other sites More sharing options...
Garethp Posted November 20, 2008 Share Posted November 20, 2008 str_replace ( mixed $search , mixed $replace , mixed $subject) For example <?php $TextM = "Hello World"; $TextM = str_replace("Hello", "Wadup", $TextM); echo $TextM; ?> Will display "Wadup World" Quote Link to comment https://forums.phpfreaks.com/topic/133450-how-to-str_replace/#findComment-694109 Share on other sites More sharing options...
dreamwest Posted November 20, 2008 Author Share Posted November 20, 2008 ive simplified the question - ive also tried this way but no luck, logically it should work Is this format a curl option?? <?php $config = "this is the message"; $config = str_replace("{config}",$config); ?> {config} Quote Link to comment https://forums.phpfreaks.com/topic/133450-how-to-str_replace/#findComment-694119 Share on other sites More sharing options...
haku Posted November 20, 2008 Share Posted November 20, 2008 No, you are using the wrong syntax. As GarethP posted, str_replace looks like this: str_replace ( mixed $search , mixed $replace , mixed $subject) As you can see, there are three variables. You are only including two variables - the thing to search for, and the thing to replace it with. But you have left out the $replace value. Quote Link to comment https://forums.phpfreaks.com/topic/133450-how-to-str_replace/#findComment-694194 Share on other sites More sharing options...
Garethp Posted November 20, 2008 Share Posted November 20, 2008 Ok, I'll make it real simple. Follow along $Newvariable = str_replace("Search for this", "Replace it with this", $Fromthisvar); So $Newvariable will take $Fromthisvar and search for all "Search for this" and replace it with "Replace with this". You can replace the search and replace with other variables. If you don't get it now, maybe you should hire someone else to do it... Quote Link to comment https://forums.phpfreaks.com/topic/133450-how-to-str_replace/#findComment-694223 Share on other sites More sharing options...
Styles2304 Posted November 20, 2008 Share Posted November 20, 2008 Can't hurt to have one more example: <?php $OldConfig = "this is the message"; $NewConfig = ("this", "that", $OldConfig); echo $NewConfig; //Outputs "that is the message" ?> Quote Link to comment https://forums.phpfreaks.com/topic/133450-how-to-str_replace/#findComment-694234 Share on other sites More sharing options...
dreamwest Posted November 20, 2008 Author Share Posted November 20, 2008 yes i understand the example, but im trying to "echo" the {variable within the page}: im trying to change $config -which is within config.php into {config}- which is in page.htm config.php holds multiple str_replace codes: <?php $config = "this is the message"; $ch = str_replace("{config}",$config,$ch); $config_2 = "config 2 message"; $ch = str_replace("{config_2}",$config_2,$ch); ?> page.htm displays php code by using {} tags <?php virtual('config.php'); ?> {config} {config_2} I pulled this code out of another script I have, $ch is curl $ch = curl_init(); curl_setopt($ch, CURLOPT_URL,$url); // set url to post to curl_setopt($ch, CURLOPT_FAILONERROR, 1); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);// allow redirects curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // return into a variable curl_setopt($ch, CURLOPT_TIMEOUT, 3); // times out after 4s curl_setopt($ch, CURLOPT_HTTPHEADER, $myHeader); curl_setopt($ch, CURLOPT_FILE, $fp); curl_exec($ch); // run the whole process curl_close($ch); maybe this isnt a str replace question?? Quote Link to comment https://forums.phpfreaks.com/topic/133450-how-to-str_replace/#findComment-694251 Share on other sites More sharing options...
haku Posted November 21, 2008 Share Posted November 21, 2008 Lets look at your function: $config = "this is the message"; $ch = str_replace("{config}",$config,$ch); With that code, you are searching for {config} inside the string $ch, and when you find it, it should be replaced with "this is the message". So if you have: $ch = "1234 {config} 567"; after running the code you posted, if you echo $ch, you will see this: 1234 this is the message 567 Because {config} has been replaced with 'this is the message'. Quote Link to comment https://forums.phpfreaks.com/topic/133450-how-to-str_replace/#findComment-695087 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.