Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. String_.php

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

String_

Namespace

PhpParser\Node\Scalar

Code

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);
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal