You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

159 lines
4.3 KiB

  1. <?php
  2. namespace dokuwiki\Utf8;
  3. /**
  4. * Methods to convert from and to UTF-8 strings
  5. */
  6. class Conversion
  7. {
  8. /**
  9. * Encodes UTF-8 characters to HTML entities
  10. *
  11. * @author Tom N Harris <tnharris@whoopdedo.org>
  12. * @author <vpribish at shopping dot com>
  13. * @link http://php.net/manual/en/function.utf8-decode.php
  14. *
  15. * @param string $str
  16. * @param bool $all Encode non-utf8 char to HTML as well
  17. * @return string
  18. */
  19. public static function toHtml($str, $all = false)
  20. {
  21. $ret = '';
  22. foreach (Unicode::fromUtf8($str) as $cp) {
  23. if ($cp < 0x80 && !$all) {
  24. $ret .= chr($cp);
  25. } elseif ($cp < 0x100) {
  26. $ret .= "&#$cp;";
  27. } else {
  28. $ret .= '&#x' . dechex($cp) . ';';
  29. }
  30. }
  31. return $ret;
  32. }
  33. /**
  34. * Decodes HTML entities to UTF-8 characters
  35. *
  36. * Convert any &#..; entity to a codepoint,
  37. * The entities flag defaults to only decoding numeric entities.
  38. * Pass HTML_ENTITIES and named entities, including &amp; &lt; etc.
  39. * are handled as well. Avoids the problem that would occur if you
  40. * had to decode "&amp;#38;&#38;amp;#38;"
  41. *
  42. * unhtmlspecialchars(\dokuwiki\Utf8\Conversion::fromHtml($s)) -> "&#38;&#38;"
  43. * \dokuwiki\Utf8\Conversion::fromHtml(unhtmlspecialchars($s)) -> "&&amp#38;"
  44. * what it should be -> "&#38;&amp#38;"
  45. *
  46. * @author Tom N Harris <tnharris@whoopdedo.org>
  47. *
  48. * @param string $str UTF-8 encoded string
  49. * @param boolean $entities decode name entities in addtition to numeric ones
  50. * @return string UTF-8 encoded string with numeric (and named) entities replaced.
  51. */
  52. public static function fromHtml($str, $entities = false)
  53. {
  54. if (!$entities) {
  55. return preg_replace_callback(
  56. '/(&#([Xx])?([0-9A-Za-z]+);)/m',
  57. [self::class, 'decodeNumericEntity'],
  58. $str
  59. );
  60. }
  61. return preg_replace_callback(
  62. '/&(#)?([Xx])?([0-9A-Za-z]+);/m',
  63. [self::class, 'decodeAnyEntity'],
  64. $str
  65. );
  66. }
  67. /**
  68. * Decodes any HTML entity to it's correct UTF-8 char equivalent
  69. *
  70. * @param string $ent An entity
  71. * @return string
  72. */
  73. protected static function decodeAnyEntity($ent)
  74. {
  75. // create the named entity lookup table
  76. static $table = null;
  77. if ($table === null) {
  78. $table = get_html_translation_table(HTML_ENTITIES);
  79. $table = array_flip($table);
  80. $table = array_map(
  81. static fn($c) => Unicode::toUtf8([ord($c)]),
  82. $table
  83. );
  84. }
  85. if ($ent[1] === '#') {
  86. return self::decodeNumericEntity($ent);
  87. }
  88. if (array_key_exists($ent[0], $table)) {
  89. return $table[$ent[0]];
  90. }
  91. return $ent[0];
  92. }
  93. /**
  94. * Decodes numeric HTML entities to their correct UTF-8 characters
  95. *
  96. * @param $ent string A numeric entity
  97. * @return string|false
  98. */
  99. protected static function decodeNumericEntity($ent)
  100. {
  101. switch ($ent[2]) {
  102. case 'X':
  103. case 'x':
  104. $cp = hexdec($ent[3]);
  105. break;
  106. default:
  107. $cp = (int) $ent[3];
  108. break;
  109. }
  110. return Unicode::toUtf8([$cp]);
  111. }
  112. /**
  113. * UTF-8 to UTF-16BE conversion.
  114. *
  115. * Maybe really UCS-2 without mb_string due to utf8_to_unicode limits
  116. *
  117. * @param string $str
  118. * @param bool $bom
  119. * @return string
  120. */
  121. public static function toUtf16be($str, $bom = false)
  122. {
  123. $out = $bom ? "\xFE\xFF" : '';
  124. if (UTF8_MBSTRING) {
  125. return $out . mb_convert_encoding($str, 'UTF-16BE', 'UTF-8');
  126. }
  127. $uni = Unicode::fromUtf8($str);
  128. foreach ($uni as $cp) {
  129. $out .= pack('n', $cp);
  130. }
  131. return $out;
  132. }
  133. /**
  134. * UTF-8 to UTF-16BE conversion.
  135. *
  136. * Maybe really UCS-2 without mb_string due to utf8_to_unicode limits
  137. *
  138. * @param string $str
  139. * @return false|string
  140. */
  141. public static function fromUtf16be($str)
  142. {
  143. $uni = unpack('n*', $str);
  144. return Unicode::toUtf8($uni);
  145. }
  146. }