Пример #1
0
/**
 * UTF-8 aware replacement for trim().
 *
 * Strip whitespace (or other characters) from the beginning and end of
 * a string.
 *
 * @param  string $str        The UTF-8 encoded string
 * @param  mixed  $stripchars The stripped characters
 * @param  int    $striptype  The optional argument $striptype can be
 *                            UTF8_STRIP_BOTH, UTF8_STRIP_LEFT, or UTF8_STRIP_RIGHT.
 *                            If $striptype is not specified it is assumed to be
 *                            UTF8_STRIP_BOTH.
 * @return string The stripped string
 */
function utf8_strip($str, $stripchars = null, $striptype = UTF8_STRIP_BOTH)
{
    static $defaults;
    global $unicode_separators_array;
    if ($stripchars === null) {
        if ($defaults === null) {
            foreach ($unicode_separators_array as $cp) {
                $defaults[] = utf8_chr($cp);
            }
        }
        $stripchars = $defaults;
    } elseif (is_array($stripchars)) {
        $chars = array();
        foreach ($stripchars as $char) {
            if (($char = utf8_get_char($char)) !== false) {
                $chars[] = $char;
            }
        }
        $stripchars = $chars;
    } else {
        $stripchars = utf8_split($stripchars, 1);
    }
    $left = $striptype & UTF8_STRIP_LEFT;
    $right = $striptype & UTF8_STRIP_RIGHT;
    $rv = $buffer = '';
    while ($char = utf8_get_char($str, $i)) {
        $state = in_array($char, $stripchars);
        if ($left) {
            if ($state) {
                continue;
            } else {
                $left = false;
            }
        }
        if ($right) {
            if ($state) {
                $buffer .= $char;
                continue;
            } else {
                $rv .= $buffer;
                $buffer = '';
            }
        }
        $rv .= $char;
    }
    return $rv;
}
Пример #2
0
 /**
  * @dataProvider providerFailingUtf8Split
  */
 public function testFailingUtf8SplitReturnValue($str, $chunklen)
 {
     $this->assertFalse(@utf8_split($str, $chunklen));
 }
Пример #3
0
/**
 * utf8_rtrim( )
 * 
 * Strip whitespace or other characters from end of a UTF-8 string
 * @since 1.3
 * 
 * @param    string $string The string to be trimmed
 * @param    string $chrs Optional characters to be stripped
 * @return   string The string with unwanted characters stripped from the right
 */
function utf8_rtrim($string = '', $chrs = '')
{
    utf8_trim_util($string, $chrs);
    $s_look = utf8_split($string);
    $len = count($s_look);
    while ($len && isset($chrs[$s_look[$len - 1]])) {
        --$len;
    }
    return utf8_substr($string, 0, $len);
}