note 29090 deleted from function.fputs by didou
| From: | didou@php.net | Date: | Sun, 09 Nov 2003 15:18:16 +0000 |
| Subject: | note 29090 deleted from function.fputs by didou | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-60170@lists.php.net to get a copy of this message | ||
Note Submitter: o_lmo (hotmail)
----
I was creating this CMS, that used a file to store data, and i ran into this problem: every line had
to represent a seperate piece of data, but the pieces of data should be allowed to contain newlines.
So i wrote these two functions:
function to_text( $string) {
// removes all newlines etc...
$string = str_replace( '\\', '\\\\', $string);
$string = str_replace( "\n", '\n', $string);
$string = str_replace( "\r", '\r', $string);
return $string;
}
function from_text( $string) {
// puts 'em back...
$string = ereg_replace( '([^\\\\])\\\\n', "\\1\n", $string);
$string = ereg_replace( '([^\\\\])\\\\r', "\\1\r", $string);
$string = str_replace( '\\\\', '\\', $string);
return $string;
}
Basically, newlines get replaced by literal \n's, carriage returns by literal \r's and
backslashes by (literal ;p) \\'s.
Hope this helps!