frog_jr Posted April 28, 2010 Share Posted April 28, 2010 I am in a brain freeze! I am trying to output strings (obtained from an array) to a file. The string contains \r and \n characters that are not to be interpreted as newlines but written as is to the file. There are also multiple \'s that can be present... Everything I've tried thus far (addslashes, quotemeta, ...) has had no effect. For example: $test = array("str\rv?\n","as\\\\n\df"); file_put_contents("Myfile",$test[0]); // first value to file file_put_contents("Myfile","\n"); // a newline file_put_contents("Myfile",$test[1]); // put another value How do I get this data into the file? Thanks Link to comment https://forums.phpfreaks.com/topic/200007-writing-file-without-rn-translation/ Share on other sites More sharing options...
anups Posted April 28, 2010 Share Posted April 28, 2010 USE "addslashes" function $test = array('str\rv?\n','as\\\\n\df'); file_put_contents("Myfile", addslashes($test[0])); // first value to file file_put_contents("Myfile","\n"); // a newline file_put_contents("Myfile",addslashes($test[1])); // put another value Link to comment https://forums.phpfreaks.com/topic/200007-writing-file-without-rn-translation/#findComment-1049736 Share on other sites More sharing options...
frog_jr Posted April 28, 2010 Author Share Posted April 28, 2010 Thanks anup, My results are that addslashes works for most cases, but seems to be failing on \r\n combinations. Link to comment https://forums.phpfreaks.com/topic/200007-writing-file-without-rn-translation/#findComment-1049745 Share on other sites More sharing options...
anups Posted April 28, 2010 Share Posted April 28, 2010 you can use addcslashes, I just tested its working fine file_put_contents("Myfile", addcslashes("TEST\r\n","\n\r")); Link to comment https://forums.phpfreaks.com/topic/200007-writing-file-without-rn-translation/#findComment-1049752 Share on other sites More sharing options...
frog_jr Posted April 28, 2010 Author Share Posted April 28, 2010 Duh!! As I sit here wondering why I didn't do the obvious! Thanks Anup! Link to comment https://forums.phpfreaks.com/topic/200007-writing-file-without-rn-translation/#findComment-1049816 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.