/libraries/vendor/joomla/string/src/phputf8/utils/ascii.php

https://gitlab.com/vitaliylukin91/idea-rating · PHP · 214 lines · 109 code · 13 blank · 92 comment · 11 complexity · ef48e741b80af2525142c69349293c8e MD5 · raw file

  1. <?php
  2. /**
  3. * Tools to help with ASCII in UTF-8
  4. *
  5. * @package utf8
  6. */
  7. //--------------------------------------------------------------------
  8. /**
  9. * Tests whether a string contains only 7bit ASCII bytes.
  10. * You might use this to conditionally check whether a string
  11. * needs handling as UTF-8 or not, potentially offering performance
  12. * benefits by using the native PHP equivalent if it's just ASCII e.g.;
  13. *
  14. * <code>
  15. * if ( utf8_is_ascii($someString) ) {
  16. * // It's just ASCII - use the native PHP version
  17. * $someString = strtolower($someString);
  18. * } else {
  19. * $someString = utf8_strtolower($someString);
  20. * }
  21. * </code>
  22. *
  23. * @param string
  24. * @return boolean TRUE if it's all ASCII
  25. * @package utf8
  26. * @see utf8_is_ascii_ctrl
  27. */
  28. function utf8_is_ascii($str) {
  29. // Search for any bytes which are outside the ASCII range...
  30. return (preg_match('/(?:[^\x00-\x7F])/',$str) !== 1);
  31. }
  32. //--------------------------------------------------------------------
  33. /**
  34. * Tests whether a string contains only 7bit ASCII bytes with device
  35. * control codes omitted. The device control codes can be found on the
  36. * second table here: http://www.w3schools.com/tags/ref_ascii.asp
  37. *
  38. * @param string
  39. * @return boolean TRUE if it's all ASCII without device control codes
  40. * @package utf8
  41. * @see utf8_is_ascii
  42. */
  43. function utf8_is_ascii_ctrl($str) {
  44. if ( strlen($str) > 0 ) {
  45. // Search for any bytes which are outside the ASCII range,
  46. // or are device control codes
  47. return (preg_match('/[^\x09\x0A\x0D\x20-\x7E]/',$str) !== 1);
  48. }
  49. return FALSE;
  50. }
  51. //--------------------------------------------------------------------
  52. /**
  53. * Strip out all non-7bit ASCII bytes
  54. * If you need to transmit a string to system which you know can only
  55. * support 7bit ASCII, you could use this function.
  56. * @param string
  57. * @return string with non ASCII bytes removed
  58. * @package utf8
  59. * @see utf8_strip_non_ascii_ctrl
  60. */
  61. function utf8_strip_non_ascii($str) {
  62. ob_start();
  63. while ( preg_match(
  64. '/^([\x00-\x7F]+)|([^\x00-\x7F]+)/S',
  65. $str, $matches) ) {
  66. if ( !isset($matches[2]) ) {
  67. echo $matches[0];
  68. }
  69. $str = substr($str, strlen($matches[0]));
  70. }
  71. $result = ob_get_contents();
  72. ob_end_clean();
  73. return $result;
  74. }
  75. //--------------------------------------------------------------------
  76. /**
  77. * Strip out device control codes in the ASCII range
  78. * which are not permitted in XML. Note that this leaves
  79. * multi-byte characters untouched - it only removes device
  80. * control codes
  81. * @see http://hsivonen.iki.fi/producing-xml/#controlchar
  82. * @param string
  83. * @return string control codes removed
  84. */
  85. function utf8_strip_ascii_ctrl($str) {
  86. ob_start();
  87. while ( preg_match(
  88. '/^([^\x00-\x08\x0B\x0C\x0E-\x1F\x7F]+)|([\x00-\x08\x0B\x0C\x0E-\x1F\x7F]+)/S',
  89. $str, $matches) ) {
  90. if ( !isset($matches[2]) ) {
  91. echo $matches[0];
  92. }
  93. $str = substr($str, strlen($matches[0]));
  94. }
  95. $result = ob_get_contents();
  96. ob_end_clean();
  97. return $result;
  98. }
  99. //--------------------------------------------------------------------
  100. /**
  101. * Strip out all non 7bit ASCII bytes and ASCII device control codes.
  102. * For a list of ASCII device control codes see the 2nd table here:
  103. * http://www.w3schools.com/tags/ref_ascii.asp
  104. *
  105. * @param string
  106. * @return boolean TRUE if it's all ASCII
  107. * @package utf8
  108. */
  109. function utf8_strip_non_ascii_ctrl($str) {
  110. ob_start();
  111. while ( preg_match(
  112. '/^([\x09\x0A\x0D\x20-\x7E]+)|([^\x09\x0A\x0D\x20-\x7E]+)/S',
  113. $str, $matches) ) {
  114. if ( !isset($matches[2]) ) {
  115. echo $matches[0];
  116. }
  117. $str = substr($str, strlen($matches[0]));
  118. }
  119. $result = ob_get_contents();
  120. ob_end_clean();
  121. return $result;
  122. }
  123. //---------------------------------------------------------------
  124. /**
  125. * Replace accented UTF-8 characters by unaccented ASCII-7 "equivalents".
  126. * The purpose of this function is to replace characters commonly found in Latin
  127. * alphabets with something more or less equivalent from the ASCII range. This can
  128. * be useful for converting a UTF-8 to something ready for a filename, for example.
  129. * Following the use of this function, you would probably also pass the string
  130. * through utf8_strip_non_ascii to clean out any other non-ASCII chars
  131. * Use the optional parameter to just deaccent lower ($case = -1) or upper ($case = 1)
  132. * letters. Default is to deaccent both cases ($case = 0)
  133. *
  134. * For a more complete implementation of transliteration, see the utf8_to_ascii package
  135. * available from the phputf8 project downloads:
  136. * http://prdownloads.sourceforge.net/phputf8
  137. *
  138. * @param string UTF-8 string
  139. * @param int (optional) -1 lowercase only, +1 uppercase only, 1 both cases
  140. * @param string UTF-8 with accented characters replaced by ASCII chars
  141. * @return string accented chars replaced with ascii equivalents
  142. * @author Andreas Gohr <andi@splitbrain.org>
  143. * @package utf8
  144. */
  145. function utf8_accents_to_ascii( $str, $case=0 ){
  146. static $UTF8_LOWER_ACCENTS = NULL;
  147. static $UTF8_UPPER_ACCENTS = NULL;
  148. if($case <= 0){
  149. if ( is_null($UTF8_LOWER_ACCENTS) ) {
  150. $UTF8_LOWER_ACCENTS = array(
  151. 'à' => 'a', 'ô' => 'o', 'ď' => 'd', 'ḟ' => 'f', 'ë' => 'e', 'š' => 's', 'ơ' => 'o',
  152. 'ß' => 'ss', 'ă' => 'a', 'ř' => 'r', 'ț' => 't', 'ň' => 'n', 'ā' => 'a', 'ķ' => 'k',
  153. 'ŝ' => 's', 'ỳ' => 'y', 'ņ' => 'n', 'ĺ' => 'l', 'ħ' => 'h', 'ṗ' => 'p', 'ó' => 'o',
  154. 'ú' => 'u', 'ě' => 'e', 'é' => 'e', 'ç' => 'c', 'ẁ' => 'w', 'ċ' => 'c', 'õ' => 'o',
  155. 'ṡ' => 's', 'ø' => 'o', 'ģ' => 'g', 'ŧ' => 't', 'ș' => 's', 'ė' => 'e', 'ĉ' => 'c',
  156. 'ś' => 's', 'î' => 'i', 'ű' => 'u', 'ć' => 'c', 'ę' => 'e', 'ŵ' => 'w', 'ṫ' => 't',
  157. 'ū' => 'u', 'č' => 'c', 'ö' => 'oe', 'è' => 'e', 'ŷ' => 'y', 'ą' => 'a', 'ł' => 'l',
  158. 'ų' => 'u', 'ů' => 'u', 'ş' => 's', 'ğ' => 'g', 'ļ' => 'l', 'ƒ' => 'f', 'ž' => 'z',
  159. 'ẃ' => 'w', 'ḃ' => 'b', 'å' => 'a', 'ì' => 'i', 'ï' => 'i', 'ḋ' => 'd', 'ť' => 't',
  160. 'ŗ' => 'r', 'ä' => 'ae', 'í' => 'i', 'ŕ' => 'r', 'ê' => 'e', 'ü' => 'ue', 'ò' => 'o',
  161. 'ē' => 'e', 'ñ' => 'n', 'ń' => 'n', 'ĥ' => 'h', 'ĝ' => 'g', 'đ' => 'd', 'ĵ' => 'j',
  162. 'ÿ' => 'y', 'ũ' => 'u', 'ŭ' => 'u', 'ư' => 'u', 'ţ' => 't', 'ý' => 'y', 'ő' => 'o',
  163. 'â' => 'a', 'ľ' => 'l', 'ẅ' => 'w', 'ż' => 'z', 'ī' => 'i', 'ã' => 'a', 'ġ' => 'g',
  164. 'ṁ' => 'm', 'ō' => 'o', 'ĩ' => 'i', 'ù' => 'u', 'į' => 'i', 'ź' => 'z', 'á' => 'a',
  165. 'û' => 'u', 'þ' => 'th', 'ð' => 'dh', 'æ' => 'ae', 'µ' => 'u', 'ĕ' => 'e',
  166. );
  167. }
  168. $str = str_replace(
  169. array_keys($UTF8_LOWER_ACCENTS),
  170. array_values($UTF8_LOWER_ACCENTS),
  171. $str
  172. );
  173. }
  174. if($case >= 0){
  175. if ( is_null($UTF8_UPPER_ACCENTS) ) {
  176. $UTF8_UPPER_ACCENTS = array(
  177. 'À' => 'A', 'Ô' => 'O', 'Ď' => 'D', 'Ḟ' => 'F', 'Ë' => 'E', 'Š' => 'S', 'Ơ' => 'O',
  178. 'Ă' => 'A', 'Ř' => 'R', 'Ț' => 'T', 'Ň' => 'N', 'Ā' => 'A', 'Ķ' => 'K',
  179. 'Ŝ' => 'S', 'Ỳ' => 'Y', 'Ņ' => 'N', 'Ĺ' => 'L', 'Ħ' => 'H', 'Ṗ' => 'P', 'Ó' => 'O',
  180. 'Ú' => 'U', 'Ě' => 'E', 'É' => 'E', 'Ç' => 'C', 'Ẁ' => 'W', 'Ċ' => 'C', 'Õ' => 'O',
  181. 'Ṡ' => 'S', 'Ø' => 'O', 'Ģ' => 'G', 'Ŧ' => 'T', 'Ș' => 'S', 'Ė' => 'E', 'Ĉ' => 'C',
  182. 'Ś' => 'S', 'Î' => 'I', 'Ű' => 'U', 'Ć' => 'C', 'Ę' => 'E', 'Ŵ' => 'W', 'Ṫ' => 'T',
  183. 'Ū' => 'U', 'Č' => 'C', 'Ö' => 'Oe', 'È' => 'E', 'Ŷ' => 'Y', 'Ą' => 'A', 'Ł' => 'L',
  184. 'Ų' => 'U', 'Ů' => 'U', 'Ş' => 'S', 'Ğ' => 'G', 'Ļ' => 'L', 'Ƒ' => 'F', 'Ž' => 'Z',
  185. 'Ẃ' => 'W', 'Ḃ' => 'B', 'Å' => 'A', 'Ì' => 'I', 'Ï' => 'I', 'Ḋ' => 'D', 'Ť' => 'T',
  186. 'Ŗ' => 'R', 'Ä' => 'Ae', 'Í' => 'I', 'Ŕ' => 'R', 'Ê' => 'E', 'Ü' => 'Ue', 'Ò' => 'O',
  187. 'Ē' => 'E', 'Ñ' => 'N', 'Ń' => 'N', 'Ĥ' => 'H', 'Ĝ' => 'G', 'Đ' => 'D', 'Ĵ' => 'J',
  188. 'Ÿ' => 'Y', 'Ũ' => 'U', 'Ŭ' => 'U', 'Ư' => 'U', 'Ţ' => 'T', 'Ý' => 'Y', 'Ő' => 'O',
  189. 'Â' => 'A', 'Ľ' => 'L', 'Ẅ' => 'W', 'Ż' => 'Z', 'Ī' => 'I', 'Ã' => 'A', 'Ġ' => 'G',
  190. 'Ṁ' => 'M', 'Ō' => 'O', 'Ĩ' => 'I', 'Ù' => 'U', 'Į' => 'I', 'Ź' => 'Z', 'Á' => 'A',
  191. 'Û' => 'U', 'Þ' => 'Th', 'Ð' => 'Dh', 'Æ' => 'Ae', 'Ĕ' => 'E',
  192. );
  193. }
  194. $str = str_replace(
  195. array_keys($UTF8_UPPER_ACCENTS),
  196. array_values($UTF8_UPPER_ACCENTS),
  197. $str
  198. );
  199. }
  200. return $str;
  201. }