Sin descripción

String.php 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. /**
  3. * This file is part of PHPWord - A pure PHP library for reading and writing
  4. * word processing documents.
  5. *
  6. * PHPWord is free software distributed under the terms of the GNU Lesser
  7. * General Public License version 3 as published by the Free Software Foundation.
  8. *
  9. * For the full copyright and license information, please read the LICENSE
  10. * file that was distributed with this source code. For the full list of
  11. * contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
  12. *
  13. * @link https://github.com/PHPOffice/PHPWord
  14. * @copyright 2010-2014 PHPWord contributors
  15. * @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
  16. */
  17. namespace PhpOffice\PhpWord\Shared;
  18. /**
  19. * Common string functions
  20. */
  21. class String
  22. {
  23. /**
  24. * Control characters array
  25. *
  26. * @var string[]
  27. */
  28. private static $controlCharacters = array();
  29. /**
  30. * Convert from OpenXML escaped control character to PHP control character
  31. *
  32. * @param string $value Value to unescape
  33. * @return string
  34. */
  35. public static function controlCharacterOOXML2PHP($value = '')
  36. {
  37. if (empty(self::$controlCharacters)) {
  38. self::buildControlCharacters();
  39. }
  40. return str_replace(array_keys(self::$controlCharacters), array_values(self::$controlCharacters), $value);
  41. }
  42. /**
  43. * Convert from PHP control character to OpenXML escaped control character
  44. *
  45. * @param string $value Value to escape
  46. * @return string
  47. */
  48. public static function controlCharacterPHP2OOXML($value = '')
  49. {
  50. if (empty(self::$controlCharacters)) {
  51. self::buildControlCharacters();
  52. }
  53. return str_replace(array_values(self::$controlCharacters), array_keys(self::$controlCharacters), $value);
  54. }
  55. /**
  56. * Check if a string contains UTF-8 data
  57. *
  58. * @param string $value
  59. * @return boolean
  60. */
  61. public static function isUTF8($value = '')
  62. {
  63. return $value === '' || preg_match('/^./su', $value) === 1;
  64. }
  65. /**
  66. * Return UTF8 encoded value
  67. *
  68. * @param string $value
  69. * @return string
  70. */
  71. public static function toUTF8($value = '')
  72. {
  73. if (!is_null($value) && !self::isUTF8($value)) {
  74. $value = utf8_encode($value);
  75. }
  76. return $value;
  77. }
  78. /**
  79. * Returns unicode from UTF8 text
  80. *
  81. * The function is splitted to reduce cyclomatic complexity
  82. *
  83. * @param string $text UTF8 text
  84. * @return string Unicode text
  85. * @since 0.11.0
  86. */
  87. public static function toUnicode($text)
  88. {
  89. return self::unicodeToEntities(self::utf8ToUnicode($text));
  90. }
  91. /**
  92. * Returns unicode array from UTF8 text
  93. *
  94. * @param string $text UTF8 text
  95. * @return array
  96. * @since 0.11.0
  97. * @link http://www.randomchaos.com/documents/?source=php_and_unicode
  98. */
  99. private static function utf8ToUnicode($text)
  100. {
  101. $unicode = array();
  102. $values = array();
  103. $lookingFor = 1;
  104. // Gets unicode for each character
  105. for ($i = 0; $i < strlen($text); $i++) {
  106. $thisValue = ord($text[$i]);
  107. if ($thisValue < 128) {
  108. $unicode[] = $thisValue;
  109. } else {
  110. if (count($values) == 0) {
  111. $lookingFor = $thisValue < 224 ? 2 : 3;
  112. }
  113. $values[] = $thisValue;
  114. if (count($values) == $lookingFor) {
  115. if ($lookingFor == 3) {
  116. $number = (($values[0] % 16) * 4096) + (($values[1] % 64) * 64) + ($values[2] % 64);
  117. } else {
  118. $number = (($values[0] % 32) * 64) + ($values[1] % 64);
  119. }
  120. $unicode[] = $number;
  121. $values = array();
  122. $lookingFor = 1;
  123. }
  124. }
  125. }
  126. return $unicode;
  127. }
  128. /**
  129. * Returns entites from unicode array
  130. *
  131. * @param array $unicode
  132. * @return string
  133. * @since 0.11.0
  134. * @link http://www.randomchaos.com/documents/?source=php_and_unicode
  135. */
  136. private static function unicodeToEntities($unicode)
  137. {
  138. $entities = '';
  139. foreach ($unicode as $value) {
  140. if ($value != 65279) {
  141. $entities .= $value > 127 ? '\uc0{\u' . $value . '}' : chr($value);
  142. }
  143. }
  144. return $entities;
  145. }
  146. /**
  147. * Return name without underscore for < 0.10.0 variable name compatibility
  148. *
  149. * @param string $value
  150. * @return string
  151. */
  152. public static function removeUnderscorePrefix($value)
  153. {
  154. if (!is_null($value)) {
  155. if (substr($value, 0, 1) == '_') {
  156. $value = substr($value, 1);
  157. }
  158. }
  159. return $value;
  160. }
  161. /**
  162. * Build control characters array.
  163. *
  164. * @return void
  165. */
  166. private static function buildControlCharacters()
  167. {
  168. for ($i = 0; $i <= 19; ++$i) {
  169. if ($i != 9 && $i != 10 && $i != 13) {
  170. $find = '_x' . sprintf('%04s', strtoupper(dechex($i))) . '_';
  171. $replace = chr($i);
  172. self::$controlCharacters[$find] = $replace;
  173. }
  174. }
  175. }
  176. }