Jump to content

how to str_replace


dreamwest

Recommended Posts

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>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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...

Link to comment
Share on other sites

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??

 

Link to comment
Share on other sites

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'.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.