以utf8格式截取的字符串编码 **

@param string $string  要截取的字符串编码
@param int $start      开始截取
@param int $length     截取的长度，默认为null，取字符串的全长
@param boolean $dot    是否显示省略号，默认为false
@return string
19323
public static function substrForUtf8($string, $start, $length = null, $dot = false)
	{
		$l = strlen($string);
		$p = $s = 0;
		if (0 !== $start)
		{
			while ($start-- && $p < $l)
			{
				$c = $string[$p];
				if ($c < "\xC0")
					$p++;
				elseif ($c < "\xE0")
					$p += 2;
				elseif ($c < "\xF0")
					$p += 3;
				elseif ($c < "\xF8")
					$p += 4;
				elseif ($c < "\xFC")
					$p += 5;
				else
					$p += 6;
			}
			$s = $p;
		}
		
		if (empty($length))
		{
			$t = substr($string, $s);
		}
		else
		{
			$i = $length;
			while ($i-- && $p < $l)
			{
				$c = $string[$p];
				if ($c < "\xC0")
					$p++;
				elseif ($c < "\xE0")
					$p += 2;
				elseif ($c < "\xF0")
					$p += 3;
				elseif ($c < "\xF8")
					$p += 4;
				elseif ($c < "\xFC")
					$p += 5;
				else
					$p += 6;
			}
			$t = substr($string, $s, $p - $s);
		}
		
		$dot && ($p < $l) && $t .= "...";
		return $t;
	}
636291
public static function strrpos($haystack, $needle, $start = 0, $encoding = 'UTF-8') {
	    if(function_exists('iconv_substr')) {
	        return iconv_strrpos($haystack, $needle, $start, $encoding);
	    } elseif(function_exists('mb_substr')) {
	        return mb_strrpos($haystack, $needle, $start, $encoding);
	    }
	    throw new \Exception('请先安装PHP iconv或mb_string扩展');
	}
269654
public static function cutstr($string, $length, $suffix = true, $charset = "utf-8", $start = 0, $dot = ' ...')
    {
        $str = str_replace(['&amp;', '&quot;', '&lt;', '&gt;'], ['&', '"', '<', '>'], $string);
        if (function_exists("mb_substr")) {
            $strcut = mb_substr($str, $start, $length, $charset);
            if (mb_strlen($str, $charset) > $length) {
                return $suffix ? $strcut . $dot : $strcut;
            }
            return $strcut;
        }
        $re = [];
        $match = [''];
        $re['utf-8'] = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
        $re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
        $re['gbk'] = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
        $re['big5'] = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";
        preg_match_all($re[$charset], $str, $match);
        $slice = join("", array_slice($match[0], $start, $length));
        $strcut = str_replace(['&', '"', '<', '>'], ['&amp;', '&quot;', '&lt;', '&gt;'], $slice);
        return $suffix ? $strcut . $dot : $strcut;
    }
916252
public static function truncate_two($str, $start, $length = null)
    {
        if (!$length) {
            $length = $start;
            $start = 0;
        }

        if (\strlen($str) <= $length) {
            return $str;
        }

        if (\function_exists('mb_substr')) {
            $str = mb_substr(strip_tags($str), $start, $length, 'utf-8');
        } else {
            $str = substr($str, $start, $length) . '...';
        }

        return $str;
    }
809358
function substr($str,$start,$len=0) {
		if ($start<0)
			$start=$this->strlen($str)+$start;
		if (!$len)
			$len=$this->strlen($str)-$start;
		return preg_match('/^.{'.$start.'}(.{0,'.$len.'})/us',$str,$match)?
			$match[1]:FALSE;
	}
562180
public function substr8($string, $start, $length = false)
    {
        if ($length === false) {
            $length = $this->strlen8($string) - $start;
        }
        if ($this->_mb_substr) {
            return mb_substr($string, $start, $length, '8bit');
        }

        return substr($string, $start, $length);
    }
33589
public static function substr(string $string, int $start, int $length = null)
    {
        if ($length === 0) {
            return '';
        }

        // Cannot omit $length, when specifying charset
        if ($length === null) {
            // save internal encoding
            $enc = mb_internal_encoding();
            mb_internal_encoding('UTF-8');
            $str = mb_substr($string, $start);
            // restore internal encoding
            mb_internal_encoding($enc);

            return $str;
        }

        return mb_substr($string, $start, $length, 'UTF-8');
    }
12331
public static function genRandomChar($length = 8)
    {
        // 密码字符集，可任意添加你需要的字符
        //$chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
        $chars = 'abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789';
        $char = '';
        for ($i = 0; $i < $length; $i++) {
            $char .= $chars[mt_rand(0, strlen($chars) - 1)];
        }
        return $char;
    }
1122407
@Override
    public String getString(long start, int length)
    {
        if (length == 0)
        {
            return "";
        }
        if (array != null)
        {
            int ps = (int) (start % size);
            int es = (int) ((start+length) % size);
            if (ps < es)
            {
                return new String(array, ps, length);
            }
            else
            {
                StringBuilder sb = new StringBuilder();
                sb.append(array, ps, size-ps);
                sb.append(array, 0, es);
                return sb.toString();
            }
        }
        else
        {
            StringBuilder sb = new StringBuilder();
            for (int ii=0;ii<length;ii++)
            {
                sb.append((char)get(start+ii));
            }
            return sb.toString();
        }
    }
1265848
public static function truncate2(string $str, int $start, int $length = null): string
    {
        if (!$length) {
            $length = $start;
            $start  = 0;
        }

        if (\strlen($str) <= $length) {
            return $str;
        }

        if (\function_exists('mb_substr')) {
            $str = \mb_substr(\strip_tags($str), $start, $length, 'utf-8');
        } else {
            $str = \substr($str, $start, $length) . '...';
        }

        return $str;
    }
404419
public static function substrCn($string, $start = 0, $length, $charset = "utf-8", $suffix = '')
    {
        if (function_exists("mb_substr")){
            return mb_substr($string, $start, $length, $charset).$suffix;
        } elseif (function_exists('iconv_substr')) {
            return iconv_substr($string, $start, $length, $charset).$suffix;
        }
        $re['utf-8']  = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
        $re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
        $re['gbk']    = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
        $re['big5']   = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";
        preg_match_all($re[$charset], $string, $match);
        $slice = join("",array_slice($match[0], $start, $length));
        return $slice.$suffix;
    }
1060667
public static function substr($str, $len, $start = 0, $encoding = 'UTF-8') {
	    if(function_exists('iconv_substr')) {
	        return iconv_substr($str, $start, $len, $encoding);
	    } elseif(function_exists('mb_substr')) {
	        return mb_substr($str, $start, $len, $encoding);
	    }
	    throw new \Exception('请先安装PHP iconv或mb_string扩展');
	}
340979
protected function readString($start)
    {
        $position = $start + 1;
        $chunk = $position;
        $length = $this->source->getLength();
        $body = $this->source->getBody();
        $code = NULL;
        $value = '';

        while (
            $position < $length &&
            ($code = $this->charCodeAt($position)) &&
            $code !== 34 &&
            $code !== 10 && $code !== 13 && $code !== 0x2028 && $code !== 0x2029
        ) {
            ++$position;

            if ($code === 92) { // \
                $value .= mb_substr($body, $chunk, $position - 1 - $chunk, 'UTF-8');
                $code = $this->charCodeAt($position);

                switch ($code) {
                    case 34:
                        $value .= '"';
                        break;
                    case 47:
                        $value .= '\/';
                        break;
                    case 92:
                        $value .= '\\';
                        break;
                    case 98:
                        $value .= '\b';
                        break;
                    case 102:
                        $value .= '\f';
                        break;
                    case 110:
                        $value .= '\n';
                        break;
                    case 114:
                        $value .= '\r';
                        break;
                    case 116:
                        $value .= '\t';
                        break;
                    case 117:
                        $charCode = $this->uniCharCode(
                            $this->charCodeAt($position + 1),
                            $this->charCodeAt($position + 2),
                            $this->charCodeAt($position + 3),
                            $this->charCodeAt($position + 4)
                        );

                        if ($charCode < 0) {
                            // @todo Throw proper exception.
                            throw new \Exception('Bad character escape sequence.');
                        }

                        $value .= $this->fromCharCode($charCode);
                        $position += 4;
                        break;
                    default:
                        // @todo Throw proper exception.
                        throw new \Exception('Bad character escape sequence.');
                }

                ++$position;
                $chunk = $position;
            }
        }

        if ($code !== 34) {
            // @todo Throw proper exception.
            throw new \Exception('Unterminated string.');
        }

        $value .= mb_substr($body, $chunk, $position - $chunk, 'UTF-8');
        return new Token(Token::STRING_TYPE, $start, $position + 1, $value);
    }
382904
protected function getStringsBetween($string, $start, $end)
    {

        $pattern = sprintf(
                '/%s(.*?)%s/', preg_quote($start), preg_quote($end)
        );

        preg_match_all($pattern, $string, $matches);

        return $matches[1];
    }
466409
public function substr($start, $length = null, $encoding = null)
    {
        if (!$this->hasMbstring()) {
            return $this->transform('\substr', $start, $length);
        }

        $encoding = $this->encoding($encoding);

        // workaround for php < 5.4.8
        if (is_null($length)) {
            $length = \mb_strlen($this->string, $encoding);
        }

        return $this->transform('\mb_substr', $start, $length, $encoding);
    }
1033400
public function read(int $length = 8192): string
    {
        return iconv($this->charset, 'UTF-8', $this->inputStream->read($length));
    }
1033492
public function remove($startIndex, $length)
    {
        $start = UTF8::substr
        (
            (string)$this->str,
            0,
            $startIndex,
            $this->str->getEncoding()
        );

        $end = UTF8::substr
        (
            (string)$this->str,
            $startIndex + $length,
            $this->str->getLength(),
            $this->str->getEncoding()
        );

        $this->str = new Str($start.$end);

        return $this;
    }