Jump to content

Recommended Posts

I have the following code to replace some values in a template

$page_contance_before = file_get_contents('includes/contact.php');                    

$postname = $_SESSION['post']['name'];
$postemail = $_SESSION['post']['email'];
$postmessage = $_SESSION['post']['message'];
$None = $_SESSION['n1'];
$Ntwo = $_SESSION['n2'];


$PAGE_VARScontact = array(
                 'NAME' => $postname,
                 'EMAIL' => $postemail,
				 'MESSAGE' => $postmessage,
				 'None' => $None,
				 'Ntwo' => $Ntwo,
               );
               
print_r($PAGE_VARScontact);

foreach ($PAGE_VARScontact as $K => $V) {       
   $page_contance = str_replace("{".$K."}",$V,$page_contance_before);
   echo "k $K - v $V <br />";
}	

 

The problem I have is only 'Ntwo' is being replaced, if I delete 'Ntwo' => $Ntwo, from the array, then only 'None' gets replaced. Why is it only replacing the last value in the array, I have the same code working else were in the site no problem!

 

James

Link to comment
https://forums.phpfreaks.com/topic/275563-str_replace-help-needed/
Share on other sites

A less resource intensive solution would be:

 

 

$search = array("{NAME}", "{EMAIL}", "{MESSAGE}", "{none}", "{Ntwo}");
$replace = array($postname, $postemail, $postmessage, $None, $Ntwo);
$page_contance = str_replace($search, $replace, $page_contance_before);
Edited by AyKay47
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.