function Lexer::stripcslashes
2 calls to Lexer::stripcslashes()
- Lexer::lexExpression in vendor/
twig/ twig/ src/ Lexer.php - Lexer::lexString in vendor/
twig/ twig/ src/ Lexer.php
File
-
vendor/
twig/ twig/ src/ Lexer.php, line 398
Class
- Lexer
- @author Fabien Potencier <fabien@symfony.com>
Namespace
TwigCode
private function stripcslashes(string $str, string $quoteType) : string {
$result = '';
$length = \strlen($str);
$i = 0;
while ($i < $length) {
if (false === ($pos = strpos($str, '\\', $i))) {
$result .= substr($str, $i);
break;
}
$result .= substr($str, $i, $pos - $i);
$i = $pos + 1;
if ($i >= $length) {
$result .= '\\';
break;
}
$nextChar = $str[$i];
if (isset(self::SPECIAL_CHARS[$nextChar])) {
$result .= self::SPECIAL_CHARS[$nextChar];
}
elseif ('\\' === $nextChar) {
$result .= $nextChar;
}
elseif ("'" === $nextChar || '"' === $nextChar) {
if ($nextChar !== $quoteType) {
trigger_deprecation('twig/twig', '3.12', 'Character "%s" should not be escaped; the "\\" character is ignored in Twig 3 but will not be in Twig 4. Please remove the extra "\\" character at position %d in "%s" at line %d.', $nextChar, $i + 1, $this->source
->getName(), $this->lineno);
}
$result .= $nextChar;
}
elseif ('#' === $nextChar && $i + 1 < $length && '{' === $str[$i + 1]) {
$result .= '#{';
++$i;
}
elseif ('x' === $nextChar && $i + 1 < $length && ctype_xdigit($str[$i + 1])) {
$hex = $str[++$i];
if ($i + 1 < $length && ctype_xdigit($str[$i + 1])) {
$hex .= $str[++$i];
}
$result .= \chr(hexdec($hex));
}
elseif (ctype_digit($nextChar) && $nextChar < '8') {
$octal = $nextChar;
while ($i + 1 < $length && ctype_digit($str[$i + 1]) && $str[$i + 1] < '8' && \strlen($octal) < 3) {
$octal .= $str[++$i];
}
$result .= \chr(octdec($octal));
}
else {
trigger_deprecation('twig/twig', '3.12', 'Character "%s" should not be escaped; the "\\" character is ignored in Twig 3 but will not be in Twig 4. Please remove the extra "\\" character at position %d in "%s" at line %d.', $nextChar, $i + 1, $this->source
->getName(), $this->lineno);
$result .= $nextChar;
}
++$i;
}
return $result;
}