Return the length of the given string.

@param string $value
@param string $encoding

@return int
551745
public static function length($value, $encoding = null)
    {
        if ($encoding !== null) {
            return mb_strlen($value, $encoding);
        }

        return mb_strlen($value);
    }
73629
protected function byteLength($string): int
    {
        if (self::$isMbStringOverload) {
            return mb_strlen($string, '8bit');
        }

        return strlen((binary) $string);
    }
870929
public function orWhereJsonLength($column, $operator, $value = null)
    {
        [$value, $operator] = $this->prepareValueAndOperator(
            $value, $operator, func_num_args() === 2
        );

        return $this->whereJsonLength($column, $operator, $value, 'or');
    }
1225503
public function filter($value)
    {
        if ($this->encodingFormat !== null) {
            return mb_strtoupper($value, $this->encodingFormat);
        }

        return mb_strtoupper($value);
    }
450667
private function getLength(string $string) : ?int
    {
        if (extension_loaded('mbstring') && ! defined('NO_PHP_EXTENSIONS')) { // constant used for tests
            if (! mb_check_encoding($string, 'UTF-8')) {
                return null;
            }

            return mb_strlen($string, 'UTF-8');
        }

        $charLength = 0;

        $length = strlen($string);

        for ($i = 0; $i < $length; $i++) {
            $charLength++;

            $char = ord($string[$i]);

            if ($char < 0x80) { // 0bbbbbbb
                continue;
            }

            if (($char & 0xE0) === 0xC0) { // 110bbbbb
                $n = 1;
            } elseif (($char & 0xF0) === 0xE0) { // 1110bbbb
                $n = 2;
            } elseif (($char & 0xF0) === 0xF0) { // 11110bbb
                $n = 3;
            } else { // invalid char
                return null;
            }

            for ($j = 0; $j < $n; $j++) {
                if (++$i === $length) { // unexpected end of string
                    return null;
                }

                if ((ord($string[$i]) & 0xC0) !== 0x80) { // does not match 10bbbbbb
                    return null;
                }
            }
        }

        return $charLength;
    }
842423
public function computeByteStreamSize(Stream $value)
    {
        $length = $value->getSize();
        $size   = $length + $this->computeVarintSize($length);

        return $size;
    }
1196126
public static function strlen($str, $encoding = 'UTF-8') {
	    if(function_exists('iconv_strlen')) {
	        return iconv_strlen($str, $encoding);
	    } elseif(function_exists('mb_strlen')) {
	        return mb_strlen($str, $encoding);
	    }
	    throw new \Exception('请先安装PHP iconv或mb_string扩展');
	}
547565
private function getEndOfParameterValue($value, $encoded = false, $firstLine = false)
    {
        if (!preg_match('/^'.self::TOKEN_REGEX.'$/D', $value)) {
            $value = '"'.$value.'"';
        }
        $prepend = '=';
        if ($encoded) {
            $prepend = '*=';
            if ($firstLine) {
                $prepend = '*='.$this->getCharset()."'".$this->getLanguage().
                    "'";
            }
        }

        return $prepend.$value;
    }
903314
protected function isAcceptLength($value)
    {
        $value = strlen(utf8_decode($value));

        if ($value < $this->min) {
            throw new ExceptionFactory('InvalidLength', [$value, 'greater', $this->min]);
        }
        else if ($value > $this->max) {
            throw new ExceptionFactory('InvalidLength', [$value, 'less', $this->max]);
        }

        return true;
    }
782276
final public static function hasLength ($value, array $options=[]) : bool
	{
		$valueLength = strlen($value);
		if (!self::hasValue($value)) new ErrorCode(1001);

		if (isset($options['max']) && ($valueLength > (int) $options['max']))
			return FALSE;

		if (isset($options['min']) && ($valueLength < (int) $options['min']))
			return FALSE;

		if (isset($options['exact']) && ($valueLength != (int) $options['exact']))
			return FALSE;

		return TRUE;
	}
326370
public static function endsWith($value, string $search, string $encoding = 'UTF-8'): bool
    {
        if (!static::isStringCastable($value)) {
            return false;
        }

        $searchlen = (int) mb_strlen($search, $encoding);
        $length = (int) mb_strlen((string) $value, $encoding);
        $end = mb_substr((string) $value, $length - $searchlen, $searchlen, $encoding);

        return $search === $end;
    }
262924
public function decode(string $value): ?int
    {
        $value = $this->hashids->decode($value);

        if (is_array($value) && sizeof($value) > 0) {
            return $value[0];
        } else {
            return null;
        }
    }
761653
public static function length($string, $charset = 'UTF-8')
    {
        $charset = self::lower($charset);

        if ($charset == 'utf-8' || $charset == 'utf8') {
            return strlen(utf8_decode($string));
        }

        if (Horde_Util::extensionExists('mbstring')) {
            $ret = @mb_strlen($string, self::_mbstringCharset($charset));
            if (!empty($ret)) {
                return $ret;
            }
        }
        if (Horde_Util::extensionExists('intl')) {
            return grapheme_strlen(
                self::convertCharset($string, $charset, 'UTF-8')
            );
        }

        return strlen($string);
    }
1205765
public static function absLength($str) {
		if (empty($str)) {
			return 0;
		}
		if (function_exists('mb_strlen')) {
			return mb_strlen($str,'utf-8');
		}
        preg_match_all("/./u", $str, $ar);
        return count($ar[0]);
	}
198831
public static int calculateEncodedStringLength(String value) {
        int encodedLen = 1;
        if (value != null) {
            int valueLength = value.length();
            for (int i = 0; i < valueLength; i++) {
                int c = value.charAt(i);
                if (c <= (0x7f - 2)) {
                    encodedLen++;
                } else if (c <= (12415 - 2)) {
                    encodedLen += 2;
                } else {
                    if (c >= 0xd800 && c <= 0xdbff) {
                        // Found a high surrogate. Verify that surrogate pair is
                        // well-formed. Low surrogate must follow high surrogate.
                        if (i + 1 < valueLength) {
                            int c2 = value.charAt(i + 1);
                            if (c2 >= 0xdc00 && c2 <= 0xdfff) {
                                i++;
                            }
                        }
                    }
                    encodedLen += 3;
                }
            }
        }
        return encodedLen;
    }
481555
public static function maxLength($value, $length)
    {
        return Validator::string()->length(null, $length)->validate($value);
    }
176928
public static function utf8($value)
    {
        if (is_string($value) && mb_detect_encoding($value, "UTF-8", TRUE) != "UTF-8") {
            return utf8_encode($value);
        } else {
            return $value;
        }
    }