PageRenderTime 37ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/library/utf8/utils/ascii.php

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