<?php
$original = "\\t";
$content = '\\t';
$replaced = preg_replace_callback(
'/\\\\(\\\\|n|r|t|v|f|"|[0-7]{1,3}|\x[0-9A-Fa-f]{1,2})/',
'replacer',
$content);
var_dump($original, $replaced);
function replacer($match) {
$map = array(
'\\\\' => "\\",
'\\n' => "\n",
'\\r' => "\r",
'\\t' => "\t",
'\\v' => "\v",
// etc for \f \$ \"
);
$match = $match[0]; // So that $match is a scalar, the full matched pattern
if (!empty($map[$match])) {
return $map[$match];
}
// Otherwise it's octal or hex notation
if ($match[1] == 'x') {
return chr(hexdec(substr($match, 2)));
}
else {
return chr(octdec(substr($match, 1)));
}
}