PageRenderTime 50ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/openconstructor/lib/third/phputf8/utils/ascii.php

http://openconstructor.googlecode.com/
PHP | 220 lines | 109 code | 13 blank | 98 comment | 11 complexity | fbb30cb009dd68e9c25973af928770d1 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.2 2007/03/15 06:56:30 sanjar 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. }