Permalink
Browse files

Reserved character escape/unescape added

  • Loading branch information...
1 parent 9bfb29b commit 7e70de6a42d883cb82db1472aa8df756b8da58eb @rotvalli committed May 26, 2011
Showing with 26 additions and 4 deletions.
  1. +26 −4 flatdb.php
View
@@ -103,14 +103,16 @@ public function fetch() {
$this->rows = count($data) - 1;
for ($r = 1; $r < $this->rows + 1; $r++) {
- $db[] = explode("|", trim($data[$r]));
+ $db[] = array_map($this->unescape, explode("|", trim($data[$r])));
}
$this->db = $db;
}
-
+
public function add_record($new_record) {
+ $new_record = array_map($this->escape, $new_record);
+
$db_file_row = implode("|", $new_record) . "\n";
$db_file = fopen($this->filename, "a");
@@ -127,12 +129,14 @@ public function add_record($new_record) {
public function modify_record($row, $column, $new_data) {
- $this->db[$row][$column] = $new_data;
+ $this->db[$row][$column] = $this->escape($new_data);
+
+ $db_file_rows = array();
$db_file_rows = implode("|", $this->column_names) . "\n";
for ($r = 0; $r < $this->rows; $r++) {
- $db_file_rows += implode("|", $this->db($r)) . "\n";
+ $db_file_rows .= implode("|", $this->db($r)) . "\n";
}
$db_file = fopen($this->filename, "w");
@@ -166,6 +170,24 @@ public function remove_record($row) {
return true;
}
+
+
+ private function escape($value){
+
+ $value = str_replace("\r\n", "\n", $value);
+ $value = str_replace("\n", "<br/>", $value);
+ $value = str_replace("|", "&#124;", $value);
+
+ return $value;
+ }
+
+ private function unescape($value){
+
+ $value = str_replace("<br/>", "\n", $value);
+ $value = str_replace("&#124;", "|", $value);
+
+ return $value;
+ }
public function __construct($filename) {
$this->filename = $filename;

0 comments on commit 7e70de6

Please sign in to comment.