function String_::parseEscapeSequences
@internal
Parses escape sequences in strings (all string types apart from single quoted).
Parameters
string $str String without quotes:
null|string $quote Quote type:
bool $parseUnicodeEscape Whether to parse PHP 7 \u escapes:
Return value
string String with escape sequences parsed
2 calls to String_::parseEscapeSequences()
- ParserAbstract::parseDocString in vendor/
nikic/ php-parser/ lib/ PhpParser/ ParserAbstract.php - String_::parse in vendor/
nikic/ php-parser/ lib/ PhpParser/ Node/ Scalar/ String_.php - @internal
File
-
vendor/
nikic/ php-parser/ lib/ PhpParser/ Node/ Scalar/ String_.php, line 101
Class
Namespace
PhpParser\Node\ScalarCode
public static function parseEscapeSequences(string $str, ?string $quote, bool $parseUnicodeEscape = true) : string {
if (null !== $quote) {
$str = str_replace('\\' . $quote, $quote, $str);
}
$extra = '';
if ($parseUnicodeEscape) {
$extra = '|u\\{([0-9a-fA-F]+)\\}';
}
return preg_replace_callback('~\\\\([\\\\$nrtfve]|[xX][0-9a-fA-F]{1,2}|[0-7]{1,3}' . $extra . ')~', function ($matches) {
$str = $matches[1];
if (isset(self::$replacements[$str])) {
return self::$replacements[$str];
}
if ('x' === $str[0] || 'X' === $str[0]) {
return chr(hexdec(substr($str, 1)));
}
if ('u' === $str[0]) {
$dec = hexdec($matches[2]);
// If it overflowed to float, treat as INT_MAX, it will throw an error anyway.
return self::codePointToUtf8(\is_int($dec) ? $dec : \PHP_INT_MAX);
}
else {
return chr(octdec($str));
}
}, $str);
}