xref: /dokuwiki/inc/utf8.php (revision 0da4ba1be8e299965722c2c8103df5789cd9e880)
1<?php
2/**
3 * UTF8 helper functions
4 *
5 * This file now only intitializes the UTF-8 capability detection and defines helper
6 * functions if needed. All actual code is in the \dokuwiki\Utf8 classes
7 *
8 * @author     Andreas Gohr <andi@splitbrain.org>
9 */
10
11use dokuwiki\Utf8\Clean;
12use dokuwiki\Utf8\Conversion;
13use dokuwiki\Utf8\PhpString;
14use dokuwiki\Utf8\Unicode;
15
16/**
17 * check for mb_string support
18 */
19if (!defined('UTF8_MBSTRING')) {
20    if (function_exists('mb_substr') && !defined('UTF8_NOMBSTRING')) {
21        define('UTF8_MBSTRING', 1);
22    } else {
23        define('UTF8_MBSTRING', 0);
24    }
25}
26
27/**
28 * Check if PREG was compiled with UTF-8 support
29 *
30 * Without this many of the functions below will not work, so this is a minimal requirement
31 */
32if (!defined('UTF8_PREGSUPPORT')) {
33    define('UTF8_PREGSUPPORT', (bool)@preg_match('/^.$/u', 'ñ'));
34}
35
36/**
37 * Check if PREG was compiled with Unicode Property support
38 *
39 * This is not required for the functions below, but might be needed in a UTF-8 aware application
40 */
41if (!defined('UTF8_PROPERTYSUPPORT')) {
42    define('UTF8_PROPERTYSUPPORT', (bool)@preg_match('/^\pL$/u', 'ñ'));
43}
44
45
46if (UTF8_MBSTRING) {
47    mb_internal_encoding('UTF-8');
48}
49
50
51if (!function_exists('utf8_isASCII')) {
52    function utf8_isASCII($str)
53    {
54        return Clean::isASCII($str);
55    }
56}
57
58
59if (!function_exists('utf8_strip')) {
60    function utf8_strip($str)
61    {
62        return Clean::strip($str);
63    }
64}
65
66if (!function_exists('utf8_check')) {
67    function utf8_check($str)
68    {
69        return Clean::isUtf8($str);
70    }
71}
72
73if (!function_exists('utf8_basename')) {
74    function utf8_basename($path, $suffix = '')
75    {
76        return PhpString::basename($path, $suffix);
77    }
78}
79
80if (!function_exists('utf8_strlen')) {
81    function utf8_strlen($str)
82    {
83        return PhpString::strlen($str);
84    }
85}
86
87if (!function_exists('utf8_substr')) {
88    function utf8_substr($str, $offset, $length = null)
89    {
90        return PhpString::substr($str, $offset, $length);
91    }
92}
93
94if (!function_exists('utf8_substr_replace')) {
95    function utf8_substr_replace($string, $replacement, $start, $length = 0)
96    {
97        return PhpString::substr_replace($string, $replacement, $start, $length);
98    }
99}
100
101if (!function_exists('utf8_ltrim')) {
102    function utf8_ltrim($str, $charlist = '')
103    {
104        return PhpString::ltrim($str, $charlist);
105    }
106}
107
108if (!function_exists('utf8_rtrim')) {
109    function utf8_rtrim($str, $charlist = '')
110    {
111        return PhpString::rtrim($str, $charlist);
112    }
113}
114
115if (!function_exists('utf8_trim')) {
116    function utf8_trim($str, $charlist = '')
117    {
118        return PhpString::trim($str, $charlist);
119    }
120}
121
122if (!function_exists('utf8_strtolower')) {
123    function utf8_strtolower($str)
124    {
125        return PhpString::strtolower($str);
126    }
127}
128
129if (!function_exists('utf8_strtoupper')) {
130    function utf8_strtoupper($str)
131    {
132        return PhpString::strtoupper($str);
133    }
134}
135
136if (!function_exists('utf8_ucfirst')) {
137    function utf8_ucfirst($str)
138    {
139        return PhpString::ucfirst($str);
140    }
141}
142
143if (!function_exists('utf8_ucwords')) {
144    function utf8_ucwords($str)
145    {
146        return PhpString::ucwords($str);
147    }
148}
149
150if (!function_exists('utf8_deaccent')) {
151    function utf8_deaccent($str, $case = 0)
152    {
153        return Clean::deaccent($str, $case);
154    }
155}
156
157if (!function_exists('utf8_romanize')) {
158    function utf8_romanize($str)
159    {
160        return Clean::romanize($str);
161    }
162}
163
164if (!function_exists('utf8_stripspecials')) {
165    function utf8_stripspecials($str, $repl = '', $additional = '')
166    {
167        return Clean::stripspecials($str, $repl, $additional);
168    }
169}
170
171if (!function_exists('utf8_strpos')) {
172    function utf8_strpos($haystack, $needle, $offset = 0)
173    {
174        return PhpString::strpos($haystack, $needle, $offset);
175    }
176}
177
178if (!function_exists('utf8_tohtml')) {
179    function utf8_tohtml($str)
180    {
181        return Conversion::toHtml($str);
182    }
183}
184
185if (!function_exists('utf8_unhtml')) {
186    function utf8_unhtml($str, $enties = false)
187    {
188        return Conversion::fromHtml($str, $enties);
189    }
190}
191
192if (!function_exists('utf8_to_unicode')) {
193    function utf8_to_unicode($str, $strict = false)
194    {
195        return Unicode::fromUtf8($str, $strict);
196    }
197}
198
199if (!function_exists('unicode_to_utf8')) {
200    function unicode_to_utf8($arr, $strict = false)
201    {
202        return Unicode::toUtf8($arr, $strict);
203    }
204}
205
206if (!function_exists('utf8_to_utf16be')) {
207    function utf8_to_utf16be($str, $bom = false)
208    {
209        return Conversion::toUtf16be($str, $bom);
210    }
211}
212
213if (!function_exists('utf16be_to_utf8')) {
214    function utf16be_to_utf8($str)
215    {
216        return Conversion::fromUtf16be($str);
217    }
218}
219
220if (!function_exists('utf8_bad_replace')) {
221    function utf8_bad_replace($str, $replace = '')
222    {
223        return Clean::replaceBadBytes($str, $replace);
224    }
225}
226
227if (!function_exists('utf8_correctIdx')) {
228    function utf8_correctIdx($str, $i, $next = false)
229    {
230        return Clean::correctIdx($str, $i, $next);
231    }
232}
233