PageRenderTime 47ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/include/utf8/utils/ascii.php

https://bitbucket.org/gencer/fluxbb
PHP | 221 lines | 104 code | 26 blank | 91 comment | 11 complexity | c72a601db239a86259493f213717d422 MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. /**
  3. * Tools to help with ASCII in UTF-8
  4. * @version $Id: ascii.php,v 1.5 2006/10/16 20:38:12 harryf Exp $
  5. * @package utf8
  6. * @subpackage ascii
  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. * @subpackage ascii
  27. * @see utf8_is_ascii_ctrl
  28. */
  29. function utf8_is_ascii($str)
  30. {
  31. // Search for any bytes which are outside the ASCII range...
  32. return (preg_match('/(?:[^\x00-\x7F])/', $str) !== 1);
  33. }
  34. /**
  35. * Tests whether a string contains only 7bit ASCII bytes with device
  36. * control codes omitted. The device control codes can be found on the
  37. * second table here: http://www.w3schools.com/tags/ref_ascii.asp
  38. *
  39. * @param string
  40. * @return boolean TRUE if it's all ASCII without device control codes
  41. * @package utf8
  42. * @subpackage ascii
  43. * @see utf8_is_ascii
  44. */
  45. function utf8_is_ascii_ctrl($str)
  46. {
  47. // Search for any bytes which are outside the ASCII range, or are device control codes
  48. if (strlen($str) > 0)
  49. return (preg_match('/[^\x09\x0A\x0D\x20-\x7E]/', $str) !== 1);
  50. return false;
  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. * @subpackage ascii
  60. * @see utf8_strip_non_ascii_ctrl
  61. */
  62. function utf8_strip_non_ascii($str)
  63. {
  64. ob_start();
  65. while (preg_match('/^([\x00-\x7F]+)|([^\x00-\x7F]+)/S', $str, $matches))
  66. {
  67. if (!isset($matches[2]))
  68. echo $matches[0];
  69. $str = substr($str, strlen($matches[0]));
  70. }
  71. $result = ob_get_contents();
  72. ob_end_clean();
  73. return $result;
  74. }
  75. /**
  76. * Strip out device control codes in the ASCII range
  77. * which are not permitted in XML. Note that this leaves
  78. * multi-byte characters untouched - it only removes device
  79. * control codes
  80. * @see http://hsivonen.iki.fi/producing-xml/#controlchar
  81. * @param string
  82. * @return string control codes removed
  83. */
  84. function utf8_strip_ascii_ctrl($str)
  85. {
  86. ob_start();
  87. while (preg_match('/^([^\x00-\x08\x0B\x0C\x0E-\x1F\x7F]+)|([\x00-\x08\x0B\x0C\x0E-\x1F\x7F]+)/S', $str, $matches))
  88. {
  89. if (!isset($matches[2]))
  90. echo $matches[0];
  91. $str = substr($str, strlen($matches[0]));
  92. }
  93. $result = ob_get_contents();
  94. ob_end_clean();
  95. return $result;
  96. }
  97. /**
  98. * Strip out all non 7bit ASCII bytes and ASCII device control codes.
  99. * For a list of ASCII device control codes see the 2nd table here:
  100. * http://www.w3schools.com/tags/ref_ascii.asp
  101. *
  102. * @param string
  103. * @return boolean TRUE if it's all ASCII
  104. * @package utf8
  105. * @subpackage ascii
  106. */
  107. function utf8_strip_non_ascii_ctrl($str)
  108. {
  109. ob_start();
  110. while (preg_match( '/^([\x09\x0A\x0D\x20-\x7E]+)|([^\x09\x0A\x0D\x20-\x7E]+)/S', $str, $matches))
  111. {
  112. if (!isset($matches[2]))
  113. echo $matches[0];
  114. $str = substr($str, strlen($matches[0]));
  115. }
  116. $result = ob_get_contents();
  117. ob_end_clean();
  118. return $result;
  119. }
  120. /**
  121. * Replace accented UTF-8 characters by unaccented ASCII-7 "equivalents".
  122. * The purpose of this function is to replace characters commonly found in Latin
  123. * alphabets with something more or less equivalent from the ASCII range. This can
  124. * be useful for converting a UTF-8 to something ready for a filename, for example.
  125. * Following the use of this function, you would probably also pass the string
  126. * through utf8_strip_non_ascii to clean out any other non-ASCII chars
  127. * Use the optional parameter to just deaccent lower ($case = -1) or upper ($case = 1)
  128. * letters. Default is to deaccent both cases ($case = 0)
  129. *
  130. * For a more complete implementation of transliteration, see the utf8_to_ascii package
  131. * available from the phputf8 project downloads:
  132. * http://prdownloads.sourceforge.net/phputf8
  133. *
  134. * @param string UTF-8 string
  135. * @param int (optional) -1 lowercase only, +1 uppercase only, 1 both cases
  136. * @param string UTF-8 with accented characters replaced by ASCII chars
  137. * @return string accented chars replaced with ascii equivalents
  138. * @author Andreas Gohr <andi@splitbrain.org>
  139. * @package utf8
  140. * @subpackage ascii
  141. */
  142. function utf8_accents_to_ascii($str, $case=0)
  143. {
  144. static $UTF8_LOWER_ACCENTS = null;
  145. static $UTF8_UPPER_ACCENTS = null;
  146. if($case <= 0)
  147. {
  148. if (is_null($UTF8_LOWER_ACCENTS))
  149. {
  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(array_keys($UTF8_LOWER_ACCENTS), array_values($UTF8_LOWER_ACCENTS), $str);
  169. }
  170. if($case >= 0)
  171. {
  172. if (is_null($UTF8_UPPER_ACCENTS))
  173. {
  174. $UTF8_UPPER_ACCENTS = array(
  175. 'À' => 'A', 'Ô' => 'O', 'Ď' => 'D', 'Ḟ' => 'F', 'Ë' => 'E', 'Š' => 'S', 'Ơ' => 'O',
  176. 'Ă' => 'A', 'Ř' => 'R', 'Ț' => 'T', 'Ň' => 'N', 'Ā' => 'A', 'Ķ' => 'K',
  177. 'Ŝ' => 'S', 'Ỳ' => 'Y', 'Ņ' => 'N', 'Ĺ' => 'L', 'Ħ' => 'H', 'Ṗ' => 'P', 'Ó' => 'O',
  178. 'Ú' => 'U', 'Ě' => 'E', 'É' => 'E', 'Ç' => 'C', 'Ẁ' => 'W', 'Ċ' => 'C', 'Õ' => 'O',
  179. 'Ṡ' => 'S', 'Ø' => 'O', 'Ģ' => 'G', 'Ŧ' => 'T', 'Ș' => 'S', 'Ė' => 'E', 'Ĉ' => 'C',
  180. 'Ś' => 'S', 'Î' => 'I', 'Ű' => 'U', 'Ć' => 'C', 'Ę' => 'E', 'Ŵ' => 'W', 'Ṫ' => 'T',
  181. 'Ū' => 'U', 'Č' => 'C', 'Ö' => 'Oe', 'È' => 'E', 'Ŷ' => 'Y', 'Ą' => 'A', 'Ł' => 'L',
  182. 'Ų' => 'U', 'Ů' => 'U', 'Ş' => 'S', 'Ğ' => 'G', 'Ļ' => 'L', 'Ƒ' => 'F', 'Ž' => 'Z',
  183. 'Ẃ' => 'W', 'Ḃ' => 'B', 'Å' => 'A', 'Ì' => 'I', 'Ï' => 'I', 'Ḋ' => 'D', 'Ť' => 'T',
  184. 'Ŗ' => 'R', 'Ä' => 'Ae', 'Í' => 'I', 'Ŕ' => 'R', 'Ê' => 'E', 'Ü' => 'Ue', 'Ò' => 'O',
  185. 'Ē' => 'E', 'Ñ' => 'N', 'Ń' => 'N', 'Ĥ' => 'H', 'Ĝ' => 'G', 'Đ' => 'D', 'Ĵ' => 'J',
  186. 'Ÿ' => 'Y', 'Ũ' => 'U', 'Ŭ' => 'U', 'Ư' => 'U', 'Ţ' => 'T', 'Ý' => 'Y', 'Ő' => 'O',
  187. 'Â' => 'A', 'Ľ' => 'L', 'Ẅ' => 'W', 'Ż' => 'Z', 'Ī' => 'I', 'Ã' => 'A', 'Ġ' => 'G',
  188. 'Ṁ' => 'M', 'Ō' => 'O', 'Ĩ' => 'I', 'Ù' => 'U', 'Į' => 'I', 'Ź' => 'Z', 'Á' => 'A',
  189. 'Û' => 'U', 'Þ' => 'Th', 'Ð' => 'Dh', 'Æ' => 'Ae', 'Ĕ' => 'E',
  190. );
  191. }
  192. $str = str_replace(array_keys($UTF8_UPPER_ACCENTS), array_values($UTF8_UPPER_ACCENTS), $str);
  193. }
  194. return $str;
  195. }