xref: /dokuwiki/inc/utf8.php (revision f41bbe4cad0871728891d9ffb45bd6fd79ab1024)
1ed7b5f09Sandi<?php
282257610Sandi/**
382257610Sandi * UTF8 helper functions
482257610Sandi *
51f2058faSAndreas Gohr * @license    LGPL 2.1 (http://www.gnu.org/copyleft/lesser.html)
682257610Sandi * @author     Andreas Gohr <andi@splitbrain.org>
782257610Sandi */
882257610Sandi
9ab77016bSAndreas Gohr/**
10ab77016bSAndreas Gohr * check for mb_string support
11ab77016bSAndreas Gohr */
12ab77016bSAndreas Gohrif (!defined('UTF8_MBSTRING')) {
13ab77016bSAndreas Gohr    if (function_exists('mb_substr') && !defined('UTF8_NOMBSTRING')) {
14ab77016bSAndreas Gohr        define('UTF8_MBSTRING', 1);
15ab77016bSAndreas Gohr    } else {
16ab77016bSAndreas Gohr        define('UTF8_MBSTRING', 0);
17ab77016bSAndreas Gohr    }
18ab77016bSAndreas Gohr}
19ab77016bSAndreas Gohr
203161005dSAndreas Gohr/**
213161005dSAndreas Gohr * Check if PREG was compiled with UTF-8 support
223161005dSAndreas Gohr *
233161005dSAndreas Gohr * Without this many of the functions below will not work, so this is a minimal requirement
243161005dSAndreas Gohr */
253161005dSAndreas Gohrif (!defined('UTF8_PREGSUPPORT')) {
263161005dSAndreas Gohr    define('UTF8_PREGSUPPORT', (bool)@preg_match('/^.$/u', 'ñ'));
273161005dSAndreas Gohr}
283161005dSAndreas Gohr
293161005dSAndreas Gohr/**
303161005dSAndreas Gohr * Check if PREG was compiled with Unicode Property support
313161005dSAndreas Gohr *
323161005dSAndreas Gohr * This is not required for the functions below, but might be needed in a UTF-8 aware application
333161005dSAndreas Gohr */
343161005dSAndreas Gohrif (!defined('UTF8_PROPERTYSUPPORT')) {
353161005dSAndreas Gohr    define('UTF8_PROPERTYSUPPORT', (bool)@preg_match('/^\pL$/u', 'ñ'));
363161005dSAndreas Gohr}
373161005dSAndreas Gohr
383161005dSAndreas Gohr
39*f41bbe4cSAndreas Gohrif (UTF8_MBSTRING) {
40*f41bbe4cSAndreas Gohr    mb_internal_encoding('UTF-8');
41*f41bbe4cSAndreas Gohr}
42*f41bbe4cSAndreas Gohr
435e613a5cSchris
44df957b36SAndreas Gohrif (!function_exists('utf8_isASCII')) {
45*f41bbe4cSAndreas Gohr    function utf8_isASCII($str)
46*f41bbe4cSAndreas Gohr    {
47*f41bbe4cSAndreas Gohr        return \dokuwiki\Utf8\Clean::isASCII($str);
4844f669e9Sandi    }
49df957b36SAndreas Gohr}
5044f669e9Sandi
51*f41bbe4cSAndreas Gohr
52df957b36SAndreas Gohrif (!function_exists('utf8_strip')) {
53*f41bbe4cSAndreas Gohr    function utf8_strip($str)
54*f41bbe4cSAndreas Gohr    {
55*f41bbe4cSAndreas Gohr        return \dokuwiki\Utf8\Clean::strip($str);
56e1906e6eSandi    }
57df957b36SAndreas Gohr}
58e1906e6eSandi
59df957b36SAndreas Gohrif (!function_exists('utf8_check')) {
60*f41bbe4cSAndreas Gohr    function utf8_check($str)
61*f41bbe4cSAndreas Gohr    {
62*f41bbe4cSAndreas Gohr        return \dokuwiki\Utf8\Clean::isUtf8($str);
63f29bd553Sandi    }
64df957b36SAndreas Gohr}
6549c713a3Sandi
66f393a4ebSAndreas Gohrif (!function_exists('utf8_basename')) {
67*f41bbe4cSAndreas Gohr    function utf8_basename($path, $suffix = '')
68*f41bbe4cSAndreas Gohr    {
69*f41bbe4cSAndreas Gohr        return \dokuwiki\Utf8\PhpString::basename($path, $suffix);
70f393a4ebSAndreas Gohr    }
71f393a4ebSAndreas Gohr}
72f393a4ebSAndreas Gohr
73df957b36SAndreas Gohrif (!function_exists('utf8_strlen')) {
74*f41bbe4cSAndreas Gohr    function utf8_strlen($str)
75*f41bbe4cSAndreas Gohr    {
76*f41bbe4cSAndreas Gohr        return \dokuwiki\Utf8\PhpString::strlen($str);
772f954959Sandi    }
78df957b36SAndreas Gohr}
792f954959Sandi
80df957b36SAndreas Gohrif (!function_exists('utf8_substr')) {
81*f41bbe4cSAndreas Gohr    function utf8_substr($str, $offset, $length = null)
82*f41bbe4cSAndreas Gohr    {
83*f41bbe4cSAndreas Gohr        return \dokuwiki\Utf8\PhpString::substr($str, $offset, $length);
842626ee0cSchris    }
85df957b36SAndreas Gohr}
8610f09f2aSAndreas Gohr
87df957b36SAndreas Gohrif (!function_exists('utf8_substr_replace')) {
88*f41bbe4cSAndreas Gohr    function utf8_substr_replace($string, $replacement, $start, $length = 0)
89*f41bbe4cSAndreas Gohr    {
90*f41bbe4cSAndreas Gohr        return \dokuwiki\Utf8\PhpString::substr_replace($string, $replacement, $start, $length);
91dc57ef04Sandi    }
92df957b36SAndreas Gohr}
93dc57ef04Sandi
94df957b36SAndreas Gohrif (!function_exists('utf8_ltrim')) {
95*f41bbe4cSAndreas Gohr    function utf8_ltrim($str, $charlist = '')
96*f41bbe4cSAndreas Gohr    {
97*f41bbe4cSAndreas Gohr        return \dokuwiki\Utf8\PhpString::ltrim($str, $charlist);
98f29317c1Sandi    }
99df957b36SAndreas Gohr}
100f29317c1Sandi
101df957b36SAndreas Gohrif (!function_exists('utf8_rtrim')) {
102*f41bbe4cSAndreas Gohr    function utf8_rtrim($str, $charlist = '')
103*f41bbe4cSAndreas Gohr    {
104*f41bbe4cSAndreas Gohr        return \dokuwiki\Utf8\PhpString::rtrim($str, $charlist);
105f29317c1Sandi    }
106df957b36SAndreas Gohr}
107f29317c1Sandi
108df957b36SAndreas Gohrif (!function_exists('utf8_trim')) {
109*f41bbe4cSAndreas Gohr    function utf8_trim($str, $charlist = '')
110*f41bbe4cSAndreas Gohr    {
111*f41bbe4cSAndreas Gohr        return \dokuwiki\Utf8\PhpString::trim($str, $charlist);
112f29317c1Sandi    }
113df957b36SAndreas Gohr}
114f29317c1Sandi
115df957b36SAndreas Gohrif (!function_exists('utf8_strtolower')) {
116*f41bbe4cSAndreas Gohr    function utf8_strtolower($str)
117*f41bbe4cSAndreas Gohr    {
118*f41bbe4cSAndreas Gohr        return \dokuwiki\Utf8\PhpString::strtolower($str);
11982257610Sandi    }
120df957b36SAndreas Gohr}
12182257610Sandi
122df957b36SAndreas Gohrif (!function_exists('utf8_strtoupper')) {
123*f41bbe4cSAndreas Gohr    function utf8_strtoupper($str)
124*f41bbe4cSAndreas Gohr    {
125*f41bbe4cSAndreas Gohr        return \dokuwiki\Utf8\PhpString::strtoupper($str);
12682257610Sandi    }
127df957b36SAndreas Gohr}
12882257610Sandi
129df957b36SAndreas Gohrif (!function_exists('utf8_ucfirst')) {
130*f41bbe4cSAndreas Gohr    function utf8_ucfirst($str)
131*f41bbe4cSAndreas Gohr    {
132*f41bbe4cSAndreas Gohr        return \dokuwiki\Utf8\PhpString::ucfirst($str);
13326ece5a7SAndreas Gohr    }
134df957b36SAndreas Gohr}
13526ece5a7SAndreas Gohr
136df957b36SAndreas Gohrif (!function_exists('utf8_ucwords')) {
137*f41bbe4cSAndreas Gohr    function utf8_ucwords($str)
138*f41bbe4cSAndreas Gohr    {
139*f41bbe4cSAndreas Gohr        return \dokuwiki\Utf8\PhpString::ucwords($str);
14026ece5a7SAndreas Gohr    }
141df957b36SAndreas Gohr}
14226ece5a7SAndreas Gohr
143df957b36SAndreas Gohrif (!function_exists('utf8_deaccent')) {
144*f41bbe4cSAndreas Gohr    function utf8_deaccent($str, $case = 0)
145*f41bbe4cSAndreas Gohr    {
146*f41bbe4cSAndreas Gohr        return \dokuwiki\Utf8\Clean::deaccent($str, $case);
14782257610Sandi    }
148df957b36SAndreas Gohr}
14982257610Sandi
150df957b36SAndreas Gohrif (!function_exists('utf8_romanize')) {
151*f41bbe4cSAndreas Gohr    function utf8_romanize($str)
152*f41bbe4cSAndreas Gohr    {
153*f41bbe4cSAndreas Gohr        return \dokuwiki\Utf8\Clean::romanize($str);
1548a831f2bSAndreas Gohr    }
155df957b36SAndreas Gohr}
1568a831f2bSAndreas Gohr
157df957b36SAndreas Gohrif (!function_exists('utf8_stripspecials')) {
158*f41bbe4cSAndreas Gohr    function utf8_stripspecials($str, $repl = '', $additional = '')
159*f41bbe4cSAndreas Gohr    {
160*f41bbe4cSAndreas Gohr        return \dokuwiki\Utf8\Clean::stripspecials($str, $repl, $additional);
161099ada41Sandi    }
162df957b36SAndreas Gohr}
163099ada41Sandi
164df957b36SAndreas Gohrif (!function_exists('utf8_strpos')) {
165*f41bbe4cSAndreas Gohr    function utf8_strpos($haystack, $needle, $offset = 0)
166*f41bbe4cSAndreas Gohr    {
167*f41bbe4cSAndreas Gohr        return \dokuwiki\Utf8\PhpString::strpos($haystack, $needle, $offset);
16872de9068SAndreas Gohr    }
169df957b36SAndreas Gohr}
170f29317c1Sandi
171df957b36SAndreas Gohrif (!function_exists('utf8_tohtml')) {
172*f41bbe4cSAndreas Gohr    function utf8_tohtml($str)
173*f41bbe4cSAndreas Gohr    {
174*f41bbe4cSAndreas Gohr        return \dokuwiki\Utf8\Conversion::toHtml($str);
1759f9fb0e5STom N Harris    }
176df957b36SAndreas Gohr}
1779f9fb0e5STom N Harris
178df957b36SAndreas Gohrif (!function_exists('utf8_unhtml')) {
179*f41bbe4cSAndreas Gohr    function utf8_unhtml($str, $enties = false)
180*f41bbe4cSAndreas Gohr    {
181*f41bbe4cSAndreas Gohr        return \dokuwiki\Utf8\Conversion::fromHtml($str, $enties);
182ea2eed85Sandi    }
183df957b36SAndreas Gohr}
184ea2eed85Sandi
185df957b36SAndreas Gohrif (!function_exists('utf8_to_unicode')) {
186*f41bbe4cSAndreas Gohr    function utf8_to_unicode($str, $strict = false)
187*f41bbe4cSAndreas Gohr    {
188*f41bbe4cSAndreas Gohr        return \dokuwiki\Utf8\Unicode::fromUtf8($str, $strict);
18982257610Sandi    }
190df957b36SAndreas Gohr}
19182257610Sandi
192df957b36SAndreas Gohrif (!function_exists('unicode_to_utf8')) {
193*f41bbe4cSAndreas Gohr    function unicode_to_utf8($arr, $strict = false)
194*f41bbe4cSAndreas Gohr    {
195*f41bbe4cSAndreas Gohr        return \dokuwiki\Utf8\Unicode::toUtf8($arr, $strict);
19682257610Sandi    }
197df957b36SAndreas Gohr}
19882257610Sandi
199df957b36SAndreas Gohrif (!function_exists('utf8_to_utf16be')) {
200*f41bbe4cSAndreas Gohr    function utf8_to_utf16be($str, $bom = false)
201*f41bbe4cSAndreas Gohr    {
202*f41bbe4cSAndreas Gohr        return \dokuwiki\Utf8\Conversion::toUtf16be($str, $bom);
20315fa0b4fSAndreas Gohr    }
204df957b36SAndreas Gohr}
20515fa0b4fSAndreas Gohr
206df957b36SAndreas Gohrif (!function_exists('utf16be_to_utf8')) {
207*f41bbe4cSAndreas Gohr    function utf16be_to_utf8($str)
208*f41bbe4cSAndreas Gohr    {
209*f41bbe4cSAndreas Gohr        return \dokuwiki\Utf8\Conversion::fromUtf16be($str);
21015fa0b4fSAndreas Gohr    }
211df957b36SAndreas Gohr}
21215fa0b4fSAndreas Gohr
213df957b36SAndreas Gohrif (!function_exists('utf8_bad_replace')) {
214*f41bbe4cSAndreas Gohr    function utf8_bad_replace($str, $replace = '')
215*f41bbe4cSAndreas Gohr    {
216*f41bbe4cSAndreas Gohr        return \dokuwiki\Utf8\Clean::replaceBadBytes($str, $replace);
2170eac1afbSAndreas Gohr    }
218df957b36SAndreas Gohr}
219ab77016bSAndreas Gohr
220df957b36SAndreas Gohrif (!function_exists('utf8_correctIdx')) {
221*f41bbe4cSAndreas Gohr    function utf8_correctIdx($str, $i, $next = false)
222*f41bbe4cSAndreas Gohr    {
223*f41bbe4cSAndreas Gohr        return \dokuwiki\Utf8\Clean::correctIdx($str, $i, $next);
2245953e889Schris    }
225df957b36SAndreas Gohr}
226