1 <?php
2 /**
3 * @package utf8
4 */
5
6 //---------------------------------------------------------------
7 /**
8 * UTF-8 aware replacement for ltrim()
9 * Note: you only need to use this if you are supplying the charlist
10 * optional arg and it contains UTF-8 characters. Otherwise ltrim will
11 * work normally on a UTF-8 string
12 * @author Andreas Gohr <andi@splitbrain.org>
13 * @see http://www.php.net/ltrim
14 * @see http://dev.splitbrain.org/view/darcs/dokuwiki/inc/utf8.php
15 * @return string
16 * @package utf8
17 */
18 function utf8_ltrim( $str, $charlist = FALSE ) {
19 if($charlist === FALSE) return ltrim($str);
20
21 //quote charlist for use in a characterclass
22 $charlist = preg_replace('!([\\\\\\-\\]\\[/^])!','\\\${1}',$charlist);
23
24 return preg_replace('/^['.$charlist.']+/u','',$str);
25 }
26
27 //---------------------------------------------------------------
28 /**
29 * UTF-8 aware replacement for rtrim()
30 * Note: you only need to use this if you are supplying the charlist
31 * optional arg and it contains UTF-8 characters. Otherwise rtrim will
32 * work normally on a UTF-8 string
33 * @author Andreas Gohr <andi@splitbrain.org>
34 * @see http://www.php.net/rtrim
35 * @see http://dev.splitbrain.org/view/darcs/dokuwiki/inc/utf8.php
36 * @return string
37 * @package utf8
38 */
39 function utf8_rtrim( $str, $charlist = FALSE ) {
40 if($charlist === FALSE) return rtrim($str);
41
42 //quote charlist for use in a characterclass
43 $charlist = preg_replace('!([\\\\\\-\\]\\[/^])!','\\\${1}',$charlist);
44
45 return preg_replace('/['.$charlist.']+$/u','',$str);
46 }
47
48 //---------------------------------------------------------------
49 /**
50 * UTF-8 aware replacement for trim()
51 * Note: you only need to use this if you are supplying the charlist
52 * optional arg and it contains UTF-8 characters. Otherwise trim will
53 * work normally on a UTF-8 string
54 * @author Andreas Gohr <andi@splitbrain.org>
55 * @see http://www.php.net/trim
56 * @see http://dev.splitbrain.org/view/darcs/dokuwiki/inc/utf8.php
57 * @return string
58 * @package utf8
59 */
60 function utf8_trim( $str, $charlist = FALSE ) {
61 if($charlist === FALSE) return trim($str);
62 return utf8_ltrim(utf8_rtrim($str, $charlist), $charlist);
63 }
64