Decodes an encoded N-Triples string. Any \-escape sequences are substituted with their decoded value.
string $str An encoded N-Triples string.:
The unencoded string.
protected function unescapeString($str) {
if (strpos($str, '\\') === false) {
return $str;
}
$mappings = array(
't' => chr(0x9),
'b' => chr(0x8),
'n' => chr(0xa),
'r' => chr(0xd),
'f' => chr(0xc),
'\\"' => chr(0x22),
'\'' => chr(0x27),
);
foreach ($mappings as $in => $out) {
$str = preg_replace('/\\x5c([' . $in . '])/', $out, $str);
}
if (stripos($str, '\\u') === false) {
return $str;
}
while (preg_match('/\\\\(U)([0-9A-F]{8})/', $str, $matches) || preg_match('/\\\\(u)([0-9A-F]{4})/', $str, $matches)) {
$no = hexdec($matches[2]);
if ($no < 128) {
$char = chr($no);
}
elseif ($no < 2048) {
$char = chr(($no >> 6) + 192) . chr(($no & 63) + 128);
}
elseif ($no < 65536) {
$char = chr(($no >> 12) + 224) . chr(($no >> 6 & 63) + 128) . chr(($no & 63) + 128);
}
elseif ($no < 2097152) {
$char = chr(($no >> 18) + 240) . chr(($no >> 12 & 63) + 128) . chr(($no >> 6 & 63) + 128) . chr(($no & 63) + 128);
}
else {
$char = '';
}
$str = str_replace('\\' . $matches[1] . $matches[2], $char, $str);
}
return $str;
}