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/**
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*
25* @param string
26* @return boolean TRUE if it's all ASCII
27* @package utf8
28* @subpackage ascii
29* @see utf8_is_ascii_ctrl
30*/
31function utf8_is_ascii($str) {
32    // Search for any bytes which are outside the ASCII range...
33    return (preg_match('/(?:[^\x00-\x7F])/',$str) !== 1);
34}
35
36//--------------------------------------------------------------------
37/**
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
41*
42* @param string
43* @return boolean TRUE if it's all ASCII without device control codes
44* @package utf8
45* @subpackage ascii
46* @see utf8_is_ascii
47*/
48function 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);
53    }
54    return FALSE;
55}
56
57//--------------------------------------------------------------------
58/**
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.
62* @param string
63* @return string with non ASCII bytes removed
64* @package utf8
65* @subpackage ascii
66* @see utf8_strip_non_ascii_ctrl
67*/
68function utf8_strip_non_ascii($str) {
69    ob_start();
70    while ( preg_match(
71        '/^([\x00-\x7F]+)|([^\x00-\x7F]+)/S',
72            $str, $matches) ) {
73        if ( !isset($matches[2]) ) {
74            echo $matches[0];
75        }
76        $str = substr($str, strlen($matches[0]));
77    }
78    $result = ob_get_contents();
79    ob_end_clean();
80    return $result;
81}
82
83//--------------------------------------------------------------------
84/**
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
88* control codes
89* @see http://hsivonen.iki.fi/producing-xml/#controlchar
90* @param string
91* @return string control codes removed
92*/
93function utf8_strip_ascii_ctrl($str) {
94    ob_start();
95    while ( preg_match(
96        '/^([^\x00-\x08\x0B\x0C\x0E-\x1F\x7F]+)|([\x00-\x08\x0B\x0C\x0E-\x1F\x7F]+)/S',
97            $str, $matches) ) {
98        if ( !isset($matches[2]) ) {
99            echo $matches[0];
100        }
101        $str = substr($str, strlen($matches[0]));
102    }
103    $result = ob_get_contents();
104    ob_end_clean();
105    return $result;
106}
107
108//--------------------------------------------------------------------
109/**
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
113*
114* @param string
115* @return boolean TRUE if it's all ASCII
116* @package utf8
117* @subpackage ascii
118*/
119function utf8_strip_non_ascii_ctrl($str) {
120    ob_start();
121    while ( preg_match(
122        '/^([\x09\x0A\x0D\x20-\x7E]+)|([^\x09\x0A\x0D\x20-\x7E]+)/S',
123            $str, $matches) ) {
124        if ( !isset($matches[2]) ) {
125            echo $matches[0];
126        }
127        $str = substr($str, strlen($matches[0]));
128    }
129    $result = ob_get_contents();
130    ob_end_clean();
131    return $result;
132}
133
134//---------------------------------------------------------------
135/**
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)
144*
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
148*
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>
154* @package utf8
155* @subpackage ascii
156*/
157function utf8_accents_to_ascii( $str, $case=0 ){
158
159    static $UTF8_LOWER_ACCENTS = NULL;
160    static $UTF8_UPPER_ACCENTS = NULL;
161
162    if($case <= 0){
163
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',
181            );
182        }
183
184        $str = str_replace(
185                array_keys($UTF8_LOWER_ACCENTS),
186                array_values($UTF8_LOWER_ACCENTS),
187                $str
188            );
189    }
190
191    if($case >= 0){
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',
209            );
210        }
211        $str = str_replace(
212                array_keys($UTF8_UPPER_ACCENTS),
213                array_values($UTF8_UPPER_ACCENTS),
214                $str
215            );
216    }
217
218    return $str;
219
220}
221