xref: /dokuwiki/inc/Utf8/Clean.php (revision 8c7c53b0321a3cd3116b8d3b2ad27863a38dece7)
1f41bbe4cSAndreas Gohr<?php
2f41bbe4cSAndreas Gohr
3f41bbe4cSAndreas Gohrnamespace dokuwiki\Utf8;
4f41bbe4cSAndreas Gohr
5f41bbe4cSAndreas Gohr/**
6f41bbe4cSAndreas Gohr * Methods to assess and clean UTF-8 strings
7f41bbe4cSAndreas Gohr */
8f41bbe4cSAndreas Gohrclass Clean
9f41bbe4cSAndreas Gohr{
10f41bbe4cSAndreas Gohr    /**
11f41bbe4cSAndreas Gohr     * Checks if a string contains 7bit ASCII only
12f41bbe4cSAndreas Gohr     *
13f41bbe4cSAndreas Gohr     * @author Andreas Haerter <andreas.haerter@dev.mail-node.com>
14f41bbe4cSAndreas Gohr     *
15f41bbe4cSAndreas Gohr     * @param string $str
16f41bbe4cSAndreas Gohr     * @return bool
17f41bbe4cSAndreas Gohr     */
18f41bbe4cSAndreas Gohr    public static function isASCII($str)
19f41bbe4cSAndreas Gohr    {
20f41bbe4cSAndreas Gohr        return (preg_match('/(?:[^\x00-\x7F])/', $str) !== 1);
21f41bbe4cSAndreas Gohr    }
22f41bbe4cSAndreas Gohr
23f41bbe4cSAndreas Gohr    /**
24f41bbe4cSAndreas Gohr     * Tries to detect if a string is in Unicode encoding
25f41bbe4cSAndreas Gohr     *
26f41bbe4cSAndreas Gohr     * @author <bmorel@ssi.fr>
27f41bbe4cSAndreas Gohr     * @link   http://php.net/manual/en/function.utf8-encode.php
28f41bbe4cSAndreas Gohr     *
29f41bbe4cSAndreas Gohr     * @param string $str
30f41bbe4cSAndreas Gohr     * @return bool
31f41bbe4cSAndreas Gohr     */
32f41bbe4cSAndreas Gohr    public static function isUtf8($str)
33f41bbe4cSAndreas Gohr    {
34f41bbe4cSAndreas Gohr        $len = strlen($str);
35f41bbe4cSAndreas Gohr        for ($i = 0; $i < $len; $i++) {
36f41bbe4cSAndreas Gohr            $b = ord($str[$i]);
37f41bbe4cSAndreas Gohr            if ($b < 0x80) continue; # 0bbbbbbb
38f41bbe4cSAndreas Gohr            elseif (($b & 0xE0) === 0xC0) $n = 1; # 110bbbbb
39f41bbe4cSAndreas Gohr            elseif (($b & 0xF0) === 0xE0) $n = 2; # 1110bbbb
40f41bbe4cSAndreas Gohr            elseif (($b & 0xF8) === 0xF0) $n = 3; # 11110bbb
41f41bbe4cSAndreas Gohr            elseif (($b & 0xFC) === 0xF8) $n = 4; # 111110bb
42f41bbe4cSAndreas Gohr            elseif (($b & 0xFE) === 0xFC) $n = 5; # 1111110b
43f41bbe4cSAndreas Gohr            else return false; # Does not match any model
44f41bbe4cSAndreas Gohr
45f41bbe4cSAndreas Gohr            for ($j = 0; $j < $n; $j++) { # n bytes matching 10bbbbbb follow ?
46f41bbe4cSAndreas Gohr                if ((++$i === $len) || ((ord($str[$i]) & 0xC0) !== 0x80))
47f41bbe4cSAndreas Gohr                    return false;
48f41bbe4cSAndreas Gohr            }
49f41bbe4cSAndreas Gohr        }
50f41bbe4cSAndreas Gohr        return true;
51f41bbe4cSAndreas Gohr    }
52f41bbe4cSAndreas Gohr
53f41bbe4cSAndreas Gohr    /**
54f41bbe4cSAndreas Gohr     * Strips all high byte chars
55f41bbe4cSAndreas Gohr     *
56f41bbe4cSAndreas Gohr     * Returns a pure ASCII7 string
57f41bbe4cSAndreas Gohr     *
58f41bbe4cSAndreas Gohr     * @author Andreas Gohr <andi@splitbrain.org>
59f41bbe4cSAndreas Gohr     *
60f41bbe4cSAndreas Gohr     * @param string $str
61f41bbe4cSAndreas Gohr     * @return string
62f41bbe4cSAndreas Gohr     */
63f41bbe4cSAndreas Gohr    public static function strip($str)
64f41bbe4cSAndreas Gohr    {
65f41bbe4cSAndreas Gohr        $ascii = '';
66f41bbe4cSAndreas Gohr        $len = strlen($str);
67f41bbe4cSAndreas Gohr        for ($i = 0; $i < $len; $i++) {
68*2401f18dSSyntaxseed            if (ord($str[$i]) < 128) {
69*2401f18dSSyntaxseed                $ascii .= $str[$i];
70f41bbe4cSAndreas Gohr            }
71f41bbe4cSAndreas Gohr        }
72f41bbe4cSAndreas Gohr        return $ascii;
73f41bbe4cSAndreas Gohr    }
74f41bbe4cSAndreas Gohr
75f41bbe4cSAndreas Gohr    /**
76f41bbe4cSAndreas Gohr     * Removes special characters (nonalphanumeric) from a UTF-8 string
77f41bbe4cSAndreas Gohr     *
78f41bbe4cSAndreas Gohr     * This function adds the controlchars 0x00 to 0x19 to the array of
79f41bbe4cSAndreas Gohr     * stripped chars (they are not included in $UTF8_SPECIAL_CHARS)
80f41bbe4cSAndreas Gohr     *
81f41bbe4cSAndreas Gohr     * @author Andreas Gohr <andi@splitbrain.org>
82f41bbe4cSAndreas Gohr     *
83f41bbe4cSAndreas Gohr     * @param  string $string The UTF8 string to strip of special chars
84f41bbe4cSAndreas Gohr     * @param  string $repl Replace special with this string
85f41bbe4cSAndreas Gohr     * @param  string $additional Additional chars to strip (used in regexp char class)
86f41bbe4cSAndreas Gohr     * @return string
87f41bbe4cSAndreas Gohr     */
88f41bbe4cSAndreas Gohr    public static function stripspecials($string, $repl = '', $additional = '')
89f41bbe4cSAndreas Gohr    {
90f41bbe4cSAndreas Gohr        static $specials = null;
91f41bbe4cSAndreas Gohr        if ($specials === null) {
92f41bbe4cSAndreas Gohr            $specials = preg_quote(Table::specialChars(), '/');
93f41bbe4cSAndreas Gohr        }
94f41bbe4cSAndreas Gohr
95f41bbe4cSAndreas Gohr        return preg_replace('/[' . $additional . '\x00-\x19' . $specials . ']/u', $repl, $string);
96f41bbe4cSAndreas Gohr    }
97f41bbe4cSAndreas Gohr
98f41bbe4cSAndreas Gohr    /**
99f41bbe4cSAndreas Gohr     * Replace bad bytes with an alternative character
100f41bbe4cSAndreas Gohr     *
101f41bbe4cSAndreas Gohr     * ASCII character is recommended for replacement char
102f41bbe4cSAndreas Gohr     *
103f41bbe4cSAndreas Gohr     * PCRE Pattern to locate bad bytes in a UTF-8 string
104f41bbe4cSAndreas Gohr     * Comes from W3 FAQ: Multilingual Forms
105f41bbe4cSAndreas Gohr     * Note: modified to include full ASCII range including control chars
106f41bbe4cSAndreas Gohr     *
107f41bbe4cSAndreas Gohr     * @author Harry Fuecks <hfuecks@gmail.com>
108f41bbe4cSAndreas Gohr     * @see http://www.w3.org/International/questions/qa-forms-utf-8
109f41bbe4cSAndreas Gohr     *
110f41bbe4cSAndreas Gohr     * @param string $str to search
111f41bbe4cSAndreas Gohr     * @param string $replace to replace bad bytes with (defaults to '?') - use ASCII
112f41bbe4cSAndreas Gohr     * @return string
113f41bbe4cSAndreas Gohr     */
114f41bbe4cSAndreas Gohr    public static function replaceBadBytes($str, $replace = '')
115f41bbe4cSAndreas Gohr    {
116f41bbe4cSAndreas Gohr        $UTF8_BAD =
117f41bbe4cSAndreas Gohr            '([\x00-\x7F]' .                          # ASCII (including control chars)
118f41bbe4cSAndreas Gohr            '|[\xC2-\xDF][\x80-\xBF]' .               # non-overlong 2-byte
119f41bbe4cSAndreas Gohr            '|\xE0[\xA0-\xBF][\x80-\xBF]' .           # excluding overlongs
120f41bbe4cSAndreas Gohr            '|[\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}' .    # straight 3-byte
121f41bbe4cSAndreas Gohr            '|\xED[\x80-\x9F][\x80-\xBF]' .           # excluding surrogates
122f41bbe4cSAndreas Gohr            '|\xF0[\x90-\xBF][\x80-\xBF]{2}' .        # planes 1-3
123f41bbe4cSAndreas Gohr            '|[\xF1-\xF3][\x80-\xBF]{3}' .            # planes 4-15
124f41bbe4cSAndreas Gohr            '|\xF4[\x80-\x8F][\x80-\xBF]{2}' .        # plane 16
125f41bbe4cSAndreas Gohr            '|(.{1}))';                               # invalid byte
126f41bbe4cSAndreas Gohr        ob_start();
127f41bbe4cSAndreas Gohr        while (preg_match('/' . $UTF8_BAD . '/S', $str, $matches)) {
128f41bbe4cSAndreas Gohr            if (!isset($matches[2])) {
129f41bbe4cSAndreas Gohr                echo $matches[0];
130f41bbe4cSAndreas Gohr            } else {
131f41bbe4cSAndreas Gohr                echo $replace;
132f41bbe4cSAndreas Gohr            }
133f41bbe4cSAndreas Gohr            $str = substr($str, strlen($matches[0]));
134f41bbe4cSAndreas Gohr        }
135f41bbe4cSAndreas Gohr        return ob_get_clean();
136f41bbe4cSAndreas Gohr    }
137f41bbe4cSAndreas Gohr
138f41bbe4cSAndreas Gohr
139f41bbe4cSAndreas Gohr    /**
140f41bbe4cSAndreas Gohr     * Replace accented UTF-8 characters by unaccented ASCII-7 equivalents
141f41bbe4cSAndreas Gohr     *
142f41bbe4cSAndreas Gohr     * Use the optional parameter to just deaccent lower ($case = -1) or upper ($case = 1)
143f41bbe4cSAndreas Gohr     * letters. Default is to deaccent both cases ($case = 0)
144f41bbe4cSAndreas Gohr     *
145f41bbe4cSAndreas Gohr     * @author Andreas Gohr <andi@splitbrain.org>
146f41bbe4cSAndreas Gohr     *
147f41bbe4cSAndreas Gohr     * @param string $string
148f41bbe4cSAndreas Gohr     * @param int $case
149f41bbe4cSAndreas Gohr     * @return string
150f41bbe4cSAndreas Gohr     */
151f41bbe4cSAndreas Gohr    public static function deaccent($string, $case = 0)
152f41bbe4cSAndreas Gohr    {
153f41bbe4cSAndreas Gohr        if ($case <= 0) {
154f41bbe4cSAndreas Gohr            $string = strtr($string, Table::lowerAccents());
155f41bbe4cSAndreas Gohr        }
156f41bbe4cSAndreas Gohr        if ($case >= 0) {
157f41bbe4cSAndreas Gohr            $string = strtr($string, Table::upperAccents());
158f41bbe4cSAndreas Gohr        }
159f41bbe4cSAndreas Gohr        return $string;
160f41bbe4cSAndreas Gohr    }
161f41bbe4cSAndreas Gohr
162f41bbe4cSAndreas Gohr    /**
163f41bbe4cSAndreas Gohr     * Romanize a non-latin string
164f41bbe4cSAndreas Gohr     *
165f41bbe4cSAndreas Gohr     * @author Andreas Gohr <andi@splitbrain.org>
166f41bbe4cSAndreas Gohr     *
167f41bbe4cSAndreas Gohr     * @param string $string
168f41bbe4cSAndreas Gohr     * @return string
169f41bbe4cSAndreas Gohr     */
170f41bbe4cSAndreas Gohr    public static function romanize($string)
171f41bbe4cSAndreas Gohr    {
172f41bbe4cSAndreas Gohr        if (self::isASCII($string)) return $string; //nothing to do
173f41bbe4cSAndreas Gohr
174f41bbe4cSAndreas Gohr        return strtr($string, Table::romanization());
175f41bbe4cSAndreas Gohr    }
176f41bbe4cSAndreas Gohr
177f41bbe4cSAndreas Gohr    /**
178f41bbe4cSAndreas Gohr     * adjust a byte index into a utf8 string to a utf8 character boundary
179f41bbe4cSAndreas Gohr     *
180f41bbe4cSAndreas Gohr     * @author       chris smith <chris@jalakai.co.uk>
181f41bbe4cSAndreas Gohr     *
182f41bbe4cSAndreas Gohr     * @param string $str utf8 character string
183f41bbe4cSAndreas Gohr     * @param int $i byte index into $str
184f41bbe4cSAndreas Gohr     * @param bool $next direction to search for boundary, false = up (current character) true = down (next character)
185f41bbe4cSAndreas Gohr     * @return int byte index into $str now pointing to a utf8 character boundary
186f41bbe4cSAndreas Gohr     */
187f41bbe4cSAndreas Gohr    public static function correctIdx($str, $i, $next = false)
188f41bbe4cSAndreas Gohr    {
189f41bbe4cSAndreas Gohr
190f41bbe4cSAndreas Gohr        if ($i <= 0) return 0;
191f41bbe4cSAndreas Gohr
192f41bbe4cSAndreas Gohr        $limit = strlen($str);
193f41bbe4cSAndreas Gohr        if ($i >= $limit) return $limit;
194f41bbe4cSAndreas Gohr
195f41bbe4cSAndreas Gohr        if ($next) {
196f41bbe4cSAndreas Gohr            while (($i < $limit) && ((ord($str[$i]) & 0xC0) === 0x80)) $i++;
197f41bbe4cSAndreas Gohr        } else {
198f41bbe4cSAndreas Gohr            while ($i && ((ord($str[$i]) & 0xC0) === 0x80)) $i--;
199f41bbe4cSAndreas Gohr        }
200f41bbe4cSAndreas Gohr
201f41bbe4cSAndreas Gohr        return $i;
202f41bbe4cSAndreas Gohr    }
203f41bbe4cSAndreas Gohr}
204