Tuesday, November 22, 2011

Escape special char

When transferring data, special characters may be used as separators of records, e.g., row or record separators. In that case you need to escape the special characters from the data, and recover them at the data receiving side. Here is PHP code for this purpose:

test("\.");

function test($s) {
$t = encode($s);
$o = decode($t);
p("s = [$s], encode(s) = [$t], decode(encode(s)) = [$o]. " . (($o == $s) ? "passed" : "failed !!!"));
}

//
// Escape row separator "\n" in a string.
// Encoding scheme:
// \ -> \\
// \n -> \.
//
function encode($s) {
return str_replace(",", "\;", str_replace("\\", "\\\\", $s));
}

//
// Decoding scheme:
// \. -> \n
// \\ -> \
// Note: can't use the following:
// return str_replace("\\\\", "\\", str_replace("\.", "\n", $s));
// because it fails for the below case:
// s = "\.". encode(s) = "\\.", decode(encode(s)) = "\n".
//
function decode($s) {
// Use "===", since "==" treats 0 as false. See http://www.php.net/manual/en/function.strpos.php
if (strpos($s, "\\") === false) return $s;

$t = "";
$len = strlen($s);
for ($i = 0; $i < $len; $i ++) {
$c = $s[$i];
if ($c == "\\") {
$d = $s[$i + 1];
if ($d == "\\") { $t .= "\\"; $i ++; }
else if ($d == ".") { $t .= "\n"; $i ++; }
else { $t .= "(error)"; } // This shouldn't happen.
}
else { $t .= $c; }
}
return $t;
}

function p($s) {
print str_replace("\n", "<br>", $s) . "<br>";
}

No comments:

Blog Archive

Followers