Support Joomla!

Joomla! 1.5 Documentation

Packages

Package: utf8

Developer Network License

The Joomla! Developer Network content is © copyright 2006 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution- NonCommercial- ShareAlike 2.5
Source code for file /phputf8/utils/ascii.php

Documentation is available at ascii.php

  1. <?php
  2. /**
  3. * Tools to help with ASCII in UTF-8
  4. * @version $Id: ascii.php,v 1.1 2006/02/26 13:17:29 harryf Exp $
  5. * @package utf8
  6. * @subpackage ascii
  7. */
  8.  
  9. //---------------------------------------------------------------
  10. /**
  11. * UTF-8 lookup table for lower case accented letters
  12. * This lookuptable defines replacements for accented characters from the ASCII-7
  13. * range. This are lower case letters only.
  14. * @author Andreas Gohr <[email protected]>
  15. * @see utf8_deaccent()
  16. * @package utf8
  17. * @subpackage ascii
  18. */
  19. $GLOBALS['UTF8_LOWER_ACCENTS'] = array(
  20.     'à' => 'a', 'ô' => 'o', 'ÄÂ?' => 'd', 'ḟ' => 'f', 'ë' => 'e', 'Å¡' => 's', 'Æ¡' => 'o',
  21.     'ß' => 'ss', 'ă' => 'a', 'Å™' => 'r', 'È›' => 't', 'ň' => 'n', 'ÄÂ?' => 'a', 'Ä·' => 'k',
  22.     'ÅÂ?' => 's', 'ỳ' => 'y', 'ņ' => 'n', 'ĺ' => 'l', 'ħ' => 'h', 'á¹—' => 'p', 'ó' => 'o',
  23.     'ú' => 'u', 'Ä›' => 'e', 'é' => 'e', 'ç' => 'c', 'áºÂ?' => 'w', 'Ä‹' => 'c', 'õ' => 'o',
  24.     'ṡ' => 's', 'ø' => 'o', 'Ä£' => 'g', 'ŧ' => 't', 'È™' => 's', 'Ä—' => 'e', 'ĉ' => 'c',
  25.     'Å›' => 's', 'î' => 'i', 'ű' => 'u', 'ć' => 'c', 'Ä™' => 'e', 'ŵ' => 'w', 'ṫ' => 't',
  26.     'Å«' => 'u', 'ÄÂ?' => 'c', 'ö' => 'oe', 'è' => 'e', 'Å·' => 'y', 'Ä…' => 'a', 'Å‚' => 'l',
  27.     'ų' => 'u', 'ů' => 'u', 'ÅŸ' => 's', 'ÄŸ' => 'g', 'ļ' => 'l', 'Æ’' => 'f', 'ž' => 'z',
  28.     'ẃ' => 'w', 'ḃ' => 'b', 'Ã¥' => 'a', 'ì' => 'i', 'ï' => 'i', 'ḋ' => 'd', 'Å¥' => 't',
  29.     'Å—' => 'r', 'ä' => 'ae', 'í' => 'i', 'Å•' => 'r', 'ê' => 'e', 'ü' => 'ue', 'ò' => 'o',
  30.     'Ä“' => 'e', 'ñ' => 'n', 'Å„' => 'n', 'Ä¥' => 'h', 'ÄÂ?' => 'g', 'Ä‘' => 'd', 'ĵ' => 'j',
  31.     'ÿ' => 'y', 'Å©' => 'u', 'Å­' => 'u', 'ư' => 'u', 'Å£' => 't', 'ý' => 'y', 'Å‘' => 'o',
  32.     'â' => 'a', 'ľ' => 'l', 'ẅ' => 'w', 'ż' => 'z', 'Ä«' => 'i', 'ã' => 'a', 'Ä¡' => 'g',
  33.     'á¹Â?' => 'm', 'ÅÂ?' => 'o', 'Ä©' => 'i', 'ù' => 'u', 'į' => 'i', 'ź' => 'z', 'á' => 'a',
  34.     'û' => 'u', 'þ' => 'th', 'ð' => 'dh', 'æ' => 'ae', 'µ' => 'u',
  35. );
  36.  
  37.  
  38. //---------------------------------------------------------------
  39. /**
  40. * UTF-8 lookup table for upper case accented letters
  41. * This lookuptable defines replacements for accented characters from the ASCII-7
  42. * range. This are upper case letters only.
  43. * @author Andreas Gohr <[email protected]>
  44. * @see utf8_deaccent()
  45. * @package utf8
  46. * @subpackage ascii
  47.  */
  48.  
  49. $GLOBALS['UTF8_UPPER_ACCENTS'] = array(
  50.     'à' => 'A', 'ô' => 'O', 'ÄÂ?' => 'D', 'ḟ' => 'F', 'ë' => 'E', 'Å¡' => 'S', 'Æ¡' => 'O',
  51.     'ß' => 'Ss', 'ă' => 'A', 'Å™' => 'R', 'È›' => 'T', 'ň' => 'N', 'ÄÂ?' => 'A', 'Ä·' => 'K',
  52.     'ÅÂ?' => 'S', 'ỳ' => 'Y', 'ņ' => 'N', 'ĺ' => 'L', 'ħ' => 'H', 'á¹—' => 'P', 'ó' => 'O',
  53.     'ú' => 'U', 'Ä›' => 'E', 'é' => 'E', 'ç' => 'C', 'áºÂ?' => 'W', 'Ä‹' => 'C', 'õ' => 'O',
  54.     'ṡ' => 'S', 'ø' => 'O', 'Ä£' => 'G', 'ŧ' => 'T', 'È™' => 'S', 'Ä—' => 'E', 'ĉ' => 'C',
  55.     'Å›' => 'S', 'î' => 'I', 'ű' => 'U', 'ć' => 'C', 'Ä™' => 'E', 'ŵ' => 'W', 'ṫ' => 'T',
  56.     'Å«' => 'U', 'ÄÂ?' => 'C', 'ö' => 'Oe', 'è' => 'E', 'Å·' => 'Y', 'Ä…' => 'A', 'Å‚' => 'L',
  57.     'ų' => 'U', 'ů' => 'U', 'ÅŸ' => 'S', 'ÄŸ' => 'G', 'ļ' => 'L', 'Æ’' => 'F', 'ž' => 'Z',
  58.     'ẃ' => 'W', 'ḃ' => 'B', 'Ã¥' => 'A', 'ì' => 'I', 'ï' => 'I', 'ḋ' => 'D', 'Å¥' => 'T',
  59.     'Å—' => 'R', 'ä' => 'Ae', 'í' => 'I', 'Å•' => 'R', 'ê' => 'E', 'ü' => 'Ue', 'ò' => 'O',
  60.     'Ä“' => 'E', 'ñ' => 'N', 'Å„' => 'N', 'Ä¥' => 'H', 'ÄÂ?' => 'G', 'Ä‘' => 'D', 'ĵ' => 'J',
  61.     'ÿ' => 'Y', 'Å©' => 'U', 'Å­' => 'U', 'ư' => 'U', 'Å£' => 'T', 'ý' => 'Y', 'Å‘' => 'O',
  62.     'â' => 'A', 'ľ' => 'L', 'ẅ' => 'W', 'ż' => 'Z', 'Ä«' => 'I', 'ã' => 'A', 'Ä¡' => 'G',
  63.     'á¹Â?' => 'M', 'ÅÂ?' => 'O', 'Ä©' => 'I', 'ù' => 'U', 'į' => 'I', 'ź' => 'Z', 'á' => 'A',
  64.     'û' => 'U', 'Þ' => 'Th', 'ÃÂ?' => 'Dh', 'Æ' => 'Ae',
  65. );
  66.  
  67. //--------------------------------------------------------------------
  68. /**
  69. * Tests whether a string contains only 7bit ASCII bytes.
  70. * You might use this to conditionally check whether a string
  71. * needs handling as UTF-8 or not, potentially offering performance
  72. * benefits by using the native PHP equivalent if it's just ASCII e.g.;
  73. *
  74. * <code>
  75. * <?php
  76. * if ( utf8_is_ascii($someString) ) {
  77. *     // It's just ASCII - use the native PHP version
  78. *     $someString = strtolower($someString);
  79. * } else {
  80. *     $someString = utf8_strtolower($someString);
  81. * }
  82. * ?>
  83. * </code>
  84. *
  85. * @param string 
  86. * @return boolean TRUE if it's all ASCII
  87. * @package utf8
  88. * @subpackage ascii
  89. * @see utf8_is_ascii_ctrl
  90. */
  91. function utf8_is_ascii($str) {
  92.     if ( strlen($str) > 0 ) {
  93.         // Search for any bytes which are outside the ASCII range...
  94.         return (preg_match('/[^\x00-\x7F]/',$str) !== 1);
  95.     }
  96.     return FALSE;
  97. }
  98.  
  99. //--------------------------------------------------------------------
  100. /**
  101. * Tests whether a string contains only 7bit ASCII bytes with device
  102. * control codes omitted. The device control codes can be found on the
  103. * second table here: http://www.w3schools.com/tags/ref_ascii.asp
  104. *
  105. * @param string 
  106. * @return boolean TRUE if it's all ASCII without device control codes
  107. * @package utf8
  108. * @subpackage ascii
  109. * @see utf8_is_ascii
  110. */
  111. function utf8_is_ascii_ctrl($str) {
  112.     if ( strlen($str) > 0 ) {
  113.         // Search for any bytes which are outside the ASCII range,
  114.         // or are device control codes
  115.         return (preg_match('/[^\x09\x0A\x0D\x20-\x7E]/',$str) !== 1);
  116.     }
  117.     return FALSE;
  118. }
  119.  
  120. //--------------------------------------------------------------------
  121. /**
  122. * Strip out all non-7bit ASCII bytes
  123. * If you need to transmit a string to system which you know can only
  124. * support 7bit ASCII, you could use this function.
  125. * @param string 
  126. * @return string with non ASCII bytes removed
  127. * @package utf8
  128. * @subpackage ascii
  129. * @see utf8_strip_non_ascii_ctrl
  130. */
  131. function utf8_strip_non_ascii($str) {
  132.     ob_start();
  133.     while ( preg_match(
  134.         '/^([\x00-\x7F]+)|([^\x00-\x7F]+)/S',
  135.             $str, $matches) ) {
  136.         if ( !isset($matches[2]) ) {
  137.             echo $matches[0];
  138.         }
  139.         $str = substr($str, strlen($matches[0]));
  140.     }
  141.     $result = ob_get_contents();
  142.     ob_end_clean();
  143.     return $result;
  144. }
  145.  
  146. //--------------------------------------------------------------------
  147. /**
  148. * Strip out all non 7bit ASCII bytes and ASCII device control codes.
  149. * For a list of ASCII device control codes see the 2nd table here:
  150. * http://www.w3schools.com/tags/ref_ascii.asp
  151. *
  152. * @param string 
  153. * @return boolean TRUE if it's all ASCII
  154. * @package utf8
  155. * @subpackage ascii
  156. */
  157. function utf8_strip_non_ascii_ctrl($str) {
  158.     ob_start();
  159.     while ( preg_match(
  160.         '/^([\x09\x0A\x0D\x20-\x7E]+)|([^\x09\x0A\x0D\x20-\x7E]+)/S',
  161.             $str, $matches) ) {
  162.         if ( !isset($matches[2]) ) {
  163.             echo $matches[0];
  164.         }
  165.         $str = substr($str, strlen($matches[0]));
  166.     }
  167.     $result = ob_get_contents();
  168.     ob_end_clean();
  169.     return $result;
  170. }
  171.  
  172. //---------------------------------------------------------------
  173. /**
  174. * Replace accented UTF-8 characters by unaccented ASCII-7 "equivalents".
  175. * The purpose of this function is to replace characters commonly found in Latin
  176. * alphabets with something more or less equivalent from the ASCII range. This can
  177. * be useful for converting a UTF-8 to something ready for a filename, for example.
  178. * Following the use of this function, you would probably also pass the string
  179. * through utf8_strip_non_ascii to clean out any other non-ASCII chars
  180. * Use the optional parameter to just deaccent lower ($case = -1) or upper ($case = 1)
  181. * letters. Default is to deaccent both cases ($case = 0)
  182. * @param string UTF-8 string
  183. * @param int (optional) -1 lowercase only, +1 uppercase only, 1 both cases
  184. * @param string UTF-8 with accented characters replaced by ASCII chars
  185. * @return string accented chars replaced with ascii equivalents
  186. * @author Andreas Gohr <[email protected]>
  187. * @package utf8
  188. * @subpackage ascii
  189. */
  190. function utf8_accents_to_ascii( $str, $case=0 ){
  191.  
  192.       if($case <= 0){
  193.         global $UTF8_LOWER_ACCENTS;
  194.         $string = str_replace(
  195.                 array_keys($UTF8_LOWER_ACCENTS),
  196.                 array_values($UTF8_LOWER_ACCENTS),
  197.                 $str
  198.             );
  199.     }
  200.  
  201.     if($case >= 0){
  202.         global $UTF8_UPPER_ACCENTS;
  203.         $string = str_replace(
  204.                 array_keys($UTF8_UPPER_ACCENTS),
  205.                 array_values($UTF8_UPPER_ACCENTS),
  206.                 $str
  207.             );
  208.     }
  209.  
  210.     return $str;
  211.  
  212. }

Documentation generated on Mon, 05 Mar 2007 20:52:29 +0000 by phpDocumentor 1.3.1