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