andyd34 Posted February 27, 2011 Share Posted February 27, 2011 I am trying to replace some fields in a file using the following code $test = array( 'NAME' => 'A N Other', //$mailName, 'MAIL' => 'someone@internet.com', //$mailEmail, 'TELE' => '01111111111', //$mailTel, 'BODY' => 'This is a test message', //$mailBody, 'TIME' => date('g:ia'), 'DATE' => date('l jS F Y'), ); $file = file_get_contents("./languages/en/messeges/new_message.txt"); $value = array(); foreach($test as $key => $val) { $value[] = str_replace('{'.$key.'}', $val, $file); } $file ="<pre>$file</pre>"; echo implode($file, $value); here is the contents of "./languages/en/messeges/new_message.txt" New message received on {DATE} at {TIME} From: {NAME} Contact Number: {TELE} Email: {MAIL} ---------------------------------------------------------------------------- {BODY} ---------------------------------------------------------------------------- Can someone please help as its echoing the complete file for every instance, 1 at a time Quote Link to comment Share on other sites More sharing options...
flolam Posted February 27, 2011 Share Posted February 27, 2011 hi change to foreach($test as $key => $val) { $file = str_replace('{'.$key.'}', $val, $file); } Quote Link to comment Share on other sites More sharing options...
andyd34 Posted February 27, 2011 Author Share Posted February 27, 2011 Thanks, works great Quote Link to comment 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.