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