1
0
mirror of https://github.com/danog/PHP-Parser.git synced 2024-12-03 17:57:59 +01:00
PHP-Parser/lib/PHPParser/Node/Scalar/String.php

58 lines
1.5 KiB
PHP
Raw Normal View History

<?php
/**
* @property string $value String value
* @property bool $isBinary Whether the string is binary (b'')
*/
2011-06-05 18:40:04 +02:00
class PHPParser_Node_Scalar_String extends PHPParser_Node_Scalar
{
/**
* Creates a String node from a string token (parses escape sequences).
*
2011-06-28 14:16:10 +02:00
* @param string $s String
* @param int $line Line
2011-06-01 22:37:10 +02:00
*
2011-06-05 18:40:04 +02:00
* @return PHPParser_Node_Scalar_String String Node
*/
2011-06-28 14:16:10 +02:00
public static function create($s, $line) {
$isBinary = false;
if ('b' === $s[0]) {
$isBinary = true;
}
if ('\'' === $s[$isBinary]) {
$s = str_replace(
array('\\\\', '\\\''),
array( '\\', '\''),
substr($s, $isBinary + 1, -1)
);
} else {
$s = self::parseEscapeSequences(substr($s, $isBinary + 1, -1));
}
2011-06-28 14:16:10 +02:00
return new self(
array(
'value' => $s, 'isBinary' => $isBinary
2011-06-28 14:16:10 +02:00
),
$line
);
}
/**
* Parses escape sequences in the content of a doubly quoted string
* or heredoc string.
*
2011-06-01 22:37:10 +02:00
* @param string $s String without quotes
*
* @return string String with escape sequences parsed
*/
public static function parseEscapeSequences($s) {
// TODO: parse hex and oct escape sequences
return str_replace(
array('\\\\', '\"', '\$', '\n', '\r', '\t', '\f', '\v'),
array( '\\', '"', '$', "\n", "\r", "\t", "\f", "\v"),
$s
);
}
}