source: branches/rsr.v5.1.dev/web/punbb/include/utf8/utils/ascii.php @ 3

Last change on this file since 3 was 3, checked in by dj3c1t, 12 years ago

passage a Fluxbb 1.4.7

File size: 7.6 KB
Line 
1<?php
2
3/**
4* Tools to help with ASCII in UTF-8
5* @version $Id: ascii.php,v 1.5 2006/10/16 20:38:12 harryf Exp $
6* @package utf8
7* @subpackage ascii
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{
33        // Search for any bytes which are outside the ASCII range...
34        return (preg_match('/(?:[^\x00-\x7F])/', $str) !== 1);
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{
50        // Search for any bytes which are outside the ASCII range, or are device control codes
51        if (strlen($str) > 0)
52                return (preg_match('/[^\x09\x0A\x0D\x20-\x7E]/', $str) !== 1);
53
54        return false;
55}
56
57/**
58* Strip out all non-7bit ASCII bytes
59* If you need to transmit a string to system which you know can only
60* support 7bit ASCII, you could use this function.
61* @param string
62* @return string with non ASCII bytes removed
63* @package utf8
64* @subpackage ascii
65* @see utf8_strip_non_ascii_ctrl
66*/
67function utf8_strip_non_ascii($str)
68{
69        ob_start();
70
71        while (preg_match('/^([\x00-\x7F]+)|([^\x00-\x7F]+)/S', $str, $matches))
72        {
73                if (!isset($matches[2]))
74                        echo $matches[0];
75
76                $str = substr($str, strlen($matches[0]));
77        }
78
79        $result = ob_get_contents();
80        ob_end_clean();
81
82        return $result;
83}
84
85/**
86* Strip out device control codes in the ASCII range
87* which are not permitted in XML. Note that this leaves
88* multi-byte characters untouched - it only removes device
89* control codes
90* @see http://hsivonen.iki.fi/producing-xml/#controlchar
91* @param string
92* @return string control codes removed
93*/
94function utf8_strip_ascii_ctrl($str)
95{
96        ob_start();
97
98        while (preg_match('/^([^\x00-\x08\x0B\x0C\x0E-\x1F\x7F]+)|([\x00-\x08\x0B\x0C\x0E-\x1F\x7F]+)/S', $str, $matches))
99        {
100                if (!isset($matches[2]))
101                        echo $matches[0];
102
103                $str = substr($str, strlen($matches[0]));
104        }
105
106        $result = ob_get_contents();
107        ob_end_clean();
108
109        return $result;
110}
111
112/**
113* Strip out all non 7bit ASCII bytes and ASCII device control codes.
114* For a list of ASCII device control codes see the 2nd table here:
115* http://www.w3schools.com/tags/ref_ascii.asp
116*
117* @param string
118* @return boolean TRUE if it's all ASCII
119* @package utf8
120* @subpackage ascii
121*/
122function utf8_strip_non_ascii_ctrl($str)
123{
124        ob_start();
125
126        while (preg_match( '/^([\x09\x0A\x0D\x20-\x7E]+)|([^\x09\x0A\x0D\x20-\x7E]+)/S', $str, $matches))
127        {
128                if (!isset($matches[2]))
129                        echo $matches[0];
130
131                $str = substr($str, strlen($matches[0]));
132        }
133
134        $result = ob_get_contents();
135        ob_end_clean();
136
137        return $result;
138}
139
140/**
141* Replace accented UTF-8 characters by unaccented ASCII-7 "equivalents".
142* The purpose of this function is to replace characters commonly found in Latin
143* alphabets with something more or less equivalent from the ASCII range. This can
144* be useful for converting a UTF-8 to something ready for a filename, for example.
145* Following the use of this function, you would probably also pass the string
146* through utf8_strip_non_ascii to clean out any other non-ASCII chars
147* Use the optional parameter to just deaccent lower ($case = -1) or upper ($case = 1)
148* letters. Default is to deaccent both cases ($case = 0)
149*
150* For a more complete implementation of transliteration, see the utf8_to_ascii package
151* available from the phputf8 project downloads:
152* http://prdownloads.sourceforge.net/phputf8
153*
154* @param string UTF-8 string
155* @param int (optional) -1 lowercase only, +1 uppercase only, 1 both cases
156* @param string UTF-8 with accented characters replaced by ASCII chars
157* @return string accented chars replaced with ascii equivalents
158* @author Andreas Gohr <andi@splitbrain.org>
159* @package utf8
160* @subpackage ascii
161*/
162function utf8_accents_to_ascii($str, $case=0)
163{
164        static $UTF8_LOWER_ACCENTS = null;
165        static $UTF8_UPPER_ACCENTS = null;
166
167        if($case <= 0)
168        {
169
170                if (is_null($UTF8_LOWER_ACCENTS))
171                {
172                        $UTF8_LOWER_ACCENTS = array(
173                                'à' => 'a', 'ÃŽ' => 'o', 'ď' => 'd', 'ᾟ' => 'f', 'ë' => 'e', 'Å¡' => 's', 'Æ¡' => 'o',
174                                'ß' => 'ss', 'ă' => 'a', 'ř' => 'r', 'ț' => 't', 'ň' => 'n', 'ā' => 'a', 'Ä·' => 'k',
175                                'ŝ' => 's', 'ỳ' => 'y', 'ņ' => 'n', 'ĺ' => 'l', 'ħ' => 'h', 'ṗ' => 'p', 'ó' => 'o',
176                                'ú' => 'u', 'ě' => 'e', 'é' => 'e', 'ç' => 'c', 'ẁ' => 'w', 'ċ' => 'c', 'õ' => 'o',
177                                'ṡ' => 's', 'Þ' => 'o', 'Ä£' => 'g', 'ŧ' => 't', 'ș' => 's', 'ė' => 'e', 'ĉ' => 'c',
178                                'ś' => 's', 'î' => 'i', 'ű' => 'u', 'ć' => 'c', 'ę' => 'e', 'ŵ' => 'w', 'ṫ' => 't',
179                                'Å«' => 'u', 'č' => 'c', 'ö' => 'oe', 'Ú' => 'e', 'Å·' => 'y', 'ą' => 'a', 'ł' => 'l',
180                                'ų' => 'u', 'ů' => 'u', 'ş' => 's', 'ğ' => 'g', 'ÄŒ' => 'l', 'ƒ' => 'f', 'ÅŸ' => 'z',
181                                'ẃ' => 'w', 'ឃ' => 'b', 'Ã¥' => 'a', 'ì' => 'i', 'ï' => 'i', 'ᾋ' => 'd', 'Å¥' => 't',
182                                'ŗ' => 'r', 'À' => 'ae', 'í' => 'i', 'ŕ' => 'r', 'ê' => 'e', 'ÃŒ' => 'ue', 'ò' => 'o',
183                                'ē' => 'e', 'ñ' => 'n', 'ń' => 'n', 'Ä¥' => 'h', 'ĝ' => 'g', 'đ' => 'd', 'ĵ' => 'j',
184                                'ÿ' => 'y', 'Å©' => 'u', 'Å­' => 'u', 'Æ°' => 'u', 'Å£' => 't', 'Ãœ' => 'y', 'ő' => 'o',
185                                'â' => 'a', 'ÄŸ' => 'l', 'ẅ' => 'w', 'ÅŒ' => 'z', 'Ä«' => 'i', 'ã' => 'a', 'Ä¡' => 'g',
186                                'ṁ' => 'm', 'ō' => 'o', 'Ä©' => 'i', 'ù' => 'u', 'į' => 'i', 'ź' => 'z', 'á' => 'a',
187                                'û' => 'u', 'ß' => 'th', 'ð' => 'dh', 'Ê' => 'ae', 'µ' => 'u', 'ĕ' => 'e',
188                                );
189                }
190
191                $str = str_replace(array_keys($UTF8_LOWER_ACCENTS), array_values($UTF8_LOWER_ACCENTS), $str);
192        }
193
194        if($case >= 0)
195        {
196                if (is_null($UTF8_UPPER_ACCENTS))
197                {
198                        $UTF8_UPPER_ACCENTS = array(
199                                'À' => 'A', 'Ô' => 'O', 'Ď' => 'D', 'ᾞ' => 'F', 'Ë' => 'E', 'Å ' => 'S', 'Æ ' => 'O',
200                                'Ă' => 'A', 'Ř' => 'R', 'Ț' => 'T', 'Ň' => 'N', 'Ā' => 'A', 'Ķ' => 'K',
201                                'Ŝ' => 'S', 'Ỳ' => 'Y', 'Ņ' => 'N', 'Ĺ' => 'L', 'ÄŠ' => 'H', 'Ṗ' => 'P', 'Ó' => 'O',
202                                'Ú' => 'U', 'Ě' => 'E', 'É' => 'E', 'Ç' => 'C', 'Ẁ' => 'W', 'Ċ' => 'C', 'Õ' => 'O',
203                                'á¹ ' => 'S', 'Ø' => 'O', 'Ä¢' => 'G', 'ÅŠ' => 'T', 'Ș' => 'S', 'Ė' => 'E', 'Ĉ' => 'C',
204                                'Ś' => 'S', 'Î' => 'I', 'Å°' => 'U', 'Ć' => 'C', 'Ę' => 'E', 'ÅŽ' => 'W', 'Ṫ' => 'T',
205                                'Ū' => 'U', 'Č' => 'C', 'Ö' => 'Oe', 'È' => 'E', 'Ŷ' => 'Y', 'Ą' => 'A', 'Ł' => 'L',
206                                'Ų' => 'U', 'Å®' => 'U', 'Ş' => 'S', 'Ğ' => 'G', 'Ä»' => 'L', 'Ƒ' => 'F', 'Åœ' => 'Z',
207                                'Ẃ' => 'W', 'ᾂ' => 'B', 'Å' => 'A', 'Ì' => 'I', 'Ï' => 'I', 'ᾊ' => 'D', 'Å€' => 'T',
208                                'Ŗ' => 'R', 'Ä' => 'Ae', 'Í' => 'I', 'Ŕ' => 'R', 'Ê' => 'E', 'Ü' => 'Ue', 'Ò' => 'O',
209                                'Ē' => 'E', 'Ñ' => 'N', 'Ń' => 'N', 'Ä€' => 'H', 'Ĝ' => 'G', 'Đ' => 'D', 'ÄŽ' => 'J',
210                                'Åž' => 'Y', 'Åš' => 'U', 'Ŭ' => 'U', 'Ư' => 'U', 'Å¢' => 'T', 'Ý' => 'Y', 'Ő' => 'O',
211                                'Â' => 'A', 'Äœ' => 'L', 'Ẅ' => 'W', 'Å»' => 'Z', 'Ī' => 'I', 'Ã' => 'A', 'Ä ' => 'G',
212                                'Ṁ' => 'M', 'Ō' => 'O', 'Äš' => 'I', 'Ù' => 'U', 'Ä®' => 'I', 'Ź' => 'Z', 'Á' => 'A',
213                                'Û' => 'U', 'Þ' => 'Th', 'Ð' => 'Dh', 'Æ' => 'Ae', 'Ĕ' => 'E',
214                                );
215                }
216
217                $str = str_replace(array_keys($UTF8_UPPER_ACCENTS), array_values($UTF8_UPPER_ACCENTS),  $str);
218        }
219
220        return $str;
221}
Note: See TracBrowser for help on using the repository browser.