Implementation strlen() function for UTF-8 encoding string.

@param   string|null    $s
@return  int|bool|null  Returns FALSE if error occurred
511755
public static function strlen($s)
	{
		if (! ReflectionTypeHint::isValid()) return false;
		if (is_null($s)) return $s;

		//since PHP-5.3.x mb_strlen() faster then strlen(utf8_decode())
		if (function_exists('mb_strlen')) return mb_strlen($s, 'utf-8');

		/*
          utf8_decode() converts characters that are not in ISO-8859-1 to '?', which, for the purpose of counting, is quite alright.
          It's much faster than iconv_strlen()
          Note: this function does not count bad UTF-8 bytes in the string - these are simply ignored
		*/
		return strlen(utf8_decode($s));

		/*
        #slowly then strlen(utf8_decode())
        if (function_exists('iconv_strlen')) return iconv_strlen($s, 'utf-8');

        #Do not count UTF-8 continuation bytes
        #return strlen(preg_replace('/[\x80-\xBF]/sSX', '', $s));

        #slowly then strlen(utf8_decode())
        preg_match_all('~.~suSX', $str, $m);
        return count($m[0]);

        #slowly then preg_match_all() + count()
        $n = 0;
        for ($i = 0, $len = strlen($s); $i < $len; $i++)
        {
            $c = ord(substr($s, $i, 1));
            if ($c < 0x80) $n++;                 #single-byte (0xxxxxx)
            elseif (($c & 0xC0) == 0xC0) $n++;   #multi-byte starting byte (11xxxxxx)
        }
        return $n;
		*/
	}
1167445
public static function unescape($s)
    {
        $out = '';
        /** @noinspection ForeachInvariantsInspection */
        for ($count = 0, $n = \strlen($s); $count < $n; $count++) {
            if ($s[$count] !== '\\') {
                $out .= $s[$count];
            } else {
                // A backslash at the end of the string - ignore it
                if ($count === ($n - 1)) {
                    break;
                }

                switch ($s[++$count]) {
                    case ')':
                    case '(':
                    case '\\':
                        $out .= $s[$count];
                        break;

                    case 'f':
                        $out .= "\x0C";
                        break;

                    case 'b':
                        $out .= "\x08";
                        break;

                    case 't':
                        $out .= "\x09";
                        break;

                    case 'r':
                        $out .= "\x0D";
                        break;

                    case 'n':
                        $out .= "\x0A";
                        break;

                    case "\r":
                        if ($count !== $n - 1 && $s[$count + 1] === "\n") {
                            $count++;
                        }
                        break;

                    case "\n":
                        break;

                    default:
                        $actualChar = \ord($s[$count]);
                        // ascii 48 = number 0
                        // ascii 57 = number 9
                        if ($actualChar >= 48 &&
                            $actualChar <= 57) {
                            $oct = '' . $s[$count];

                            /** @noinspection NotOptimalIfConditionsInspection */
                            if ($count + 1 < $n &&
                                \ord($s[$count + 1]) >= 48 &&
                                \ord($s[$count + 1]) <= 57
                            ) {
                                $count++;
                                $oct .= $s[$count];

                                /** @noinspection NotOptimalIfConditionsInspection */
                                if ($count + 1 < $n &&
                                    \ord($s[$count + 1]) >= 48 &&
                                    \ord($s[$count + 1]) <= 57
                                ) {
                                    $oct .= $s[++$count];
                                }
                            }

                            $out .= \chr(\octdec($oct));
                        } else {
                            // If the character is not one of those defined, the backslash is ignored
                            $out .= $s[$count];
                        }
                }
            }
        }
        return $out;
    }
8036
public function writeLongStr($s)
    {
        $this->flushBits();
        $this->writeLong(strlen($s));
        $this->out .= $s;
        return $this;
    }
965994
private static function utf8ToUnicode($str)
    {
        $unicode = array();
        $values = array();
        $lookingFor = 1;

        for ($i = 0; $i < strlen($str); $i++ ) {
            $thisValue = ord($str[$i]);

            if ($thisValue < 128) {
                $unicode[] = $thisValue;
            }
            else {
                if (count($values) == 0) {
                    $lookingFor = $thisValue < 224 ? 2 : 3;
                }

                $values[] = $thisValue;

                if (count($values) == $lookingFor) {
                    $number = $lookingFor == 3
                              ? (($values[0] % 16) * 4096) + (($values[1] % 64) * 64) + ($values[2] % 64)
                              : (($values[0] % 32) * 64) + ($values[1] % 64);

                    $unicode[] = $number;
                    $values = array();
                    $lookingFor = 1;
                }
            }
        }

        return $unicode;
    }
1150825
private function _transliterateFrench ($s)
    {        
        $table = array (
            'â' => 'a',
            'ê' => 'e',
            'î' => 'i',
            'ô' => 'o',
            'û' => 'u',
            'Â' => 'A',
            'Ê' => 'E',
            'Î' => 'I',
            'Ô' => 'O',
            'Û' => 'U',
            'œ' => 'oe',
            'æ' => 'ae',
            'Ÿ' => 'Y',
            'ç' => 'c',
        	'Ç' => 'C',
        );
        return strtr($s, $table);
    }
741815
function clean_string($str)
	{
		if ($this->_is_ascii($str) === FALSE)
		{
			$str = @iconv('UTF-8', 'UTF-8//IGNORE', $str);
		}

		return $str;
	}
566185
public static function len(string $string): int
    {
        if (false === $encoding = \mb_detect_encoding($string, null, true)) {
            return \strlen($string);
        }

        return \mb_strwidth($string, $encoding);
    }
220955
public function length($str, $encoding = null)
	{
		$encoding or $encoding = $this->encoding;

		return function_exists('mb_strlen')
			? mb_strlen($str, $encoding)
			: strlen($str);
	}
957047
protected static function strlen($text) 
    {
        $exits = (function_exists('mb_strlen') && ((int) ini_get('mbstring.func_overload')) & 2);

        return $exits ? mb_strlen($text,'8bit') : strlen($text);
    }
909854
public static function maxCharExpression(string $s, int $m, ?string $e = null) : string
    {
        $e = (($e === null) ? "" : $e);
        return (mb_strlen($s) >= $m) ? mb_substr($s, 0, $m - mb_strlen($e)) . $e : $s . $e;
    }
574178
public function convert(string $string): string
    {
        if ($this->encoding === null) {
            $this->encoding = $this->getEncoding($string);
        }

        if ($this->encoding === self::UTF16BE) {
            $string = mb_convert_encoding($string, "UTF-8", "UTF-16BE");
        }

        if ($this->encoding === self::UTF16LE) {
            $string = mb_convert_encoding($string, "UTF-8", "UTF-16LE");
        }

        return $string;
    }
1143213
public static String decodeString(String s) {

        try {
            return new String(CODEC.decode(s), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            // shouldn't happen
            return null;
        }
    }
652793
private function _out_string_encoding( &$str )
     {
        $rsc = $this->_source_result_charset();
        if( $rsc==1 ) {
            $rsstr = iconv(UCS2, 'utf-8', $str);
        }
        else if( $rsc==2 ) {
            $rsstr = iconv('utf-8', 'gb18030', iconv(UCS2, 'utf-8', $str) );
        }
        else{
            $rsstr = iconv('utf-8', 'big5', iconv(UCS2, 'utf-8', $str) );
        }
        return $rsstr;
     }
76272
protected static function safeStrlen($string)
    {
        // Optimization -- only search once:
        static $exists = null;
        if ($exists === null) $exists = \function_exists('mb_strlen');
        if ($exists) return \mb_strlen($string, '8bit');
        return \strlen($string);
    }
431454
public static function toBinaryString(string $character) : string
    {
        $result = null;
        $length = strlen($character); // Note: We want the raw length, not the mb length.

        for ($i = 0; $i < $length; ++$i) {
            $result .= sprintf('%08b', ord($character[$i]));
        }

        return $result;
    }
1052979
function unicodeToUtf8() : SplString
    {

        $string = preg_replace_callback( '/\\\\u([0-9a-f]{4})/i', function( $matches ){
            return mb_convert_encoding( pack( "H*", $matches[1] ), "UTF-8", "UCS-2BE" );
        }, $this->__toString() );
        return $this->setString( $string );
    }
1170269
public static function escapeAssertion($s)
    {
        if (0 === strpos($s, 'r.')) {
            $s = substr_replace($s, 'r_', 0, 2);
        }
        if (0 === strpos($s, 'p.')) {
            $s = substr_replace($s, 'p_', 0, 2);
        }

        $s = preg_replace_callback("~(\|| |=|\)|\(|&|<|>|,|\+|-|!|\*|\/)(r|p)(\.)~", function ($m) {
            return $m[1].$m[2].'_';
        }, $s);

        return $s;
    }