note 29090 added to function.fputs
| From: | (hotmail) | Date: | Sat, 01 Feb 2003 19:02:43 +0000 |
| Subject: | note 29090 added to function.fputs | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-43265@lists.php.net to get a copy of this message | ||
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!
--
http://www.php.net/manual/en/function.fputs.php
http://master.php.net/manage/user-notes.php?action=edit+29090
http://master.php.net/manage/user-notes.php?action=delete+29090
http://master.php.net/manage/user-notes.php?action=reject+29090