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

Documentation generated on Thu, 08 Jan 2009 17:37:31 +0100 by phpDocumentor 1.4.0a2