PageRenderTime 5ms CodeModel.GetById 2ms app.highlight 1ms RepoModel.GetById 1ms app.codeStats 0ms

/libraries/phputf8/trim.php

https://gitlab.com/vitaliylukin91/idea-rating
PHP | 68 lines | 15 code | 7 blank | 46 comment | 3 complexity | 35ac4732e647d596a038d47daeb7b9eb MD5 | raw file
 1<?php
 2/**
 3* @version $Id$
 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*/
21function 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*/
43function 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*/
65function utf8_trim( $str, $charlist = FALSE ) {
66    if($charlist === FALSE) return trim($str);
67    return utf8_ltrim(utf8_rtrim($str, $charlist), $charlist);
68}