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' => '[email protected]', //$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 Link to comment https://forums.phpfreaks.com/topic/229040-str_replace-array/ 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); } Link to comment https://forums.phpfreaks.com/topic/229040-str_replace-array/#findComment-1180405 Share on other sites More sharing options...
andyd34 Posted February 27, 2011 Author Share Posted February 27, 2011 Thanks, works great Link to comment https://forums.phpfreaks.com/topic/229040-str_replace-array/#findComment-1180410 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.