You are here

public static function Utf8::decode in Extensible BBCode 4.0.x

Same name and namespace in other branches
  1. 8.3 src/Utf8.php \Drupal\xbbcode\Utf8::decode()

Decode \uXXXX and \UXXXXXXXX sequences in a UTF8 string.

Parameters

string $string: The string to decode.

Return value

string The decoded string.

1 call to Utf8::decode()
CodeTagPlugin::doProcess in standard/src/Plugin/XBBCode/CodeTagPlugin.php
Create the actual output.

File

src/Utf8.php, line 145

Class

Utf8
Implementation of UTF-8 character utilities.

Namespace

Drupal\xbbcode

Code

public static function decode(string $string) : string {

  // Decode sequences with an odd number of backslashes.
  $string = (string) preg_replace_callback('/(?<!\\\\)((?:\\\\\\\\)*)\\\\(u[\\da-fA-F]{4}|U[\\da-fA-F]{8})/', static function ($match) {
    $prefix = str_repeat('\\', strlen($match[1]) / 2);
    $code = substr($match[2], 1);
    return $prefix . self::chr(hexdec($code));
  }, $string);

  // Remove backslashes from escaped escape sequences.
  return preg_replace_callback('/(\\\\+)(u[\\da-fA-F]{4}|U[\\da-fA-F]{8})/', static function ($match) {
    return substr($match[1], strlen($match[1]) / 2) . $match[2];
  }, $string);
}