String.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. /**
  3. * @property string $value String value
  4. */
  5. class PHPParser_Node_Scalar_String extends PHPParser_Node_Scalar
  6. {
  7. protected static $replacements = array(
  8. '\\' => '\\',
  9. '$' => '$',
  10. 'n' => "\n",
  11. 'r' => "\r",
  12. 't' => "\t",
  13. 'f' => "\f",
  14. 'v' => "\v",
  15. 'e' => "\x1B",
  16. );
  17. /**
  18. * Constructs a string scalar node.
  19. *
  20. * @param string $value Value of the string
  21. * @param array $attributes Additional attributes
  22. */
  23. public function __construct($value = '', array $attributes = array()) {
  24. parent::__construct(
  25. array(
  26. 'value' => $value
  27. ),
  28. $attributes
  29. );
  30. }
  31. /**
  32. * Parses a string token.
  33. *
  34. * @param string $str String token content
  35. *
  36. * @return string The parsed string
  37. */
  38. public static function parse($str) {
  39. $bLength = 0;
  40. if ('b' === $str[0]) {
  41. $bLength = 1;
  42. }
  43. if ('\'' === $str[$bLength]) {
  44. return str_replace(
  45. array('\\\\', '\\\''),
  46. array( '\\', '\''),
  47. substr($str, $bLength + 1, -1)
  48. );
  49. } else {
  50. return self::parseEscapeSequences(substr($str, $bLength + 1, -1), '"');
  51. }
  52. }
  53. /**
  54. * Parses escape sequences in strings (all string types apart from single quoted).
  55. *
  56. * @param string $str String without quotes
  57. * @param null|string $quote Quote type
  58. *
  59. * @return string String with escape sequences parsed
  60. */
  61. public static function parseEscapeSequences($str, $quote) {
  62. if (null !== $quote) {
  63. $str = str_replace('\\' . $quote, $quote, $str);
  64. }
  65. return preg_replace_callback(
  66. '~\\\\([\\\\$nrtfve]|[xX][0-9a-fA-F]{1,2}|[0-7]{1,3})~',
  67. array(__CLASS__, 'parseCallback'),
  68. $str
  69. );
  70. }
  71. public static function parseCallback($matches) {
  72. $str = $matches[1];
  73. if (isset(self::$replacements[$str])) {
  74. return self::$replacements[$str];
  75. } elseif ('x' === $str[0] || 'X' === $str[0]) {
  76. return chr(hexdec($str));
  77. } else {
  78. return chr(octdec($str));
  79. }
  80. }
  81. /**
  82. * Parses a constant doc string.
  83. *
  84. * @param string $startToken Doc string start token content (<<<SMTHG)
  85. * @param string $str String token content
  86. *
  87. * @return string Parsed string
  88. */
  89. public static function parseDocString($startToken, $str) {
  90. // strip last newline (thanks tokenizer for sticking it into the string!)
  91. $str = preg_replace('~(\r\n|\n|\r)$~', '', $str);
  92. // nowdoc string
  93. if (false !== strpos($startToken, '\'')) {
  94. return $str;
  95. }
  96. return self::parseEscapeSequences($str, null);
  97. }
  98. }