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

Benjamin Mako Hill || Want to submit a patch?