Get UTF-8 string from (compressed or uncompressed) UTF-16 string

@param string $string
@param bool $compressed
@return string
486882
private static function encodeUTF16($string, $compressed = '')
    {
        if ($compressed) {
            $string = self::uncompressByteString($string);
        }

        return PHPExcel_Shared_String::ConvertEncoding($string, 'UTF-8', 'UTF-16LE');
    }
1274408
private function getEncoding(string &$string): string
    {
        # Check for the UTF-8 byte order mark
        if (substr($string, 0, 3) === self::UTF8) {
            $string = substr($string, 3);
            return self::UTF8;
        }

        # Check for the UTF-16 big endian byte order mark
        if (substr($string, 0, 2) === self::UTF16BE) {
            $string = substr($string, 2);
            return self::UTF16BE;
        }

        # Check for the UTF-16 little endian byte order mark
        if (substr($string, 0, 2) === self::UTF16LE) {
            $string = substr($string, 2);
            return self::UTF16LE;
        }

        return self::UNKNOWN;
    }
842607
protected function strlen($string)
    {
        // don't count color codes
        $string = preg_replace("/\33\\[\\d+(;\\d+)?m/", '', $string);

        if (function_exists('mb_strlen')) {
            return mb_strlen($string, 'utf-8');
        }

        return strlen($string);
    }
1011755
public function encodeString(string $string, ?string $charset = 'utf-8', int $firstLineOffset = 0, int $maxLineLength = 0): string
    {
        if ($maxLineLength > 76 || $maxLineLength <= 0) {
            $maxLineLength = 76;
        }

        $thisLineLength = $maxLineLength - $firstLineOffset;

        $lines = [];
        $lNo = 0;
        $lines[$lNo] = '';
        $currentLine = &$lines[$lNo++];
        $size = $lineLen = 0;
        $charStream = new CharacterStream($string, $charset);

        // Fetching more than 4 chars at one is slower, as is fetching fewer bytes
        // Conveniently 4 chars is the UTF-8 safe number since UTF-8 has up to 6
        // bytes per char and (6 * 4 * 3 = 72 chars per line) * =NN is 3 bytes
        while (null !== $bytes = $charStream->readBytes(4)) {
            $enc = $this->encodeByteSequence($bytes, $size);

            $i = strpos($enc, '=0D=0A');
            $newLineLength = $lineLen + (false === $i ? $size : $i);

            if ($currentLine && $newLineLength >= $thisLineLength) {
                $lines[$lNo] = '';
                $currentLine = &$lines[$lNo++];
                $thisLineLength = $maxLineLength;
                $lineLen = 0;
            }

            $currentLine .= $enc;

            if (false === $i) {
                $lineLen += $size;
            } else {
                // 6 is the length of '=0D=0A'.
                $lineLen = $size - strrpos($enc, '=0D=0A') - 6;
            }
        }

        return $this->standardize(implode("=\r\n", $lines));
    }
514458
public static function safeBase64Decode($string)
    {
        $data = str_replace(['-', '_'], ['+', '/'], $string);
        $mod4 = strlen($data) % 4;
        if ($mod4) {
            $data .= substr('====', $mod4);
        }
        return base64_decode($data);
    }
607476
public static function encodeUtf8ToFileSystem($string){
		if (preg_match('//u', $string) && strtoupper(substr(php_uname('s'), 0, 7)) === 'WINDOWS') {
			return iconv('utf-8', 'cp1252//IGNORE' ,  $string);
		}
		return $string;
	}
626741
public static function utf8ToCodepoint( $char ) {
		# Find the length
		$z = ord( $char[0] );
		if ( $z & 0x80 ) {
			$length = 0;
			while ( $z & 0x80 ) {
				$length++;
				$z <<= 1;
			}
		} else {
			$length = 1;
		}

		if ( $length != strlen( $char ) ) {
			return false;
		}

		if ( $length == 1 ) {
			return ord( $char );
		}

		# Mask off the length-determining bits and shift back to the original location
		$z &= 0xff;
		$z >>= $length;

		# Add in the free bits from subsequent bytes
		for ( $i = 1; $i < $length; $i++ ) {
			$z <<= 6;
			$z |= ord( $char[$i] ) & 0x3f;
		}

		return $z;
	}
1339441
public static byte[] getBytesUTF8(String string)
    {
        if (string == null)
        {
            return null;
        }

        try
        {
            return string.getBytes("UTF-8");
        }
        catch (UnsupportedEncodingException e)
        {
            // This never happens.
            return null;
        }
    }
1191767
function md5_16($string)
    {
        $tmp = md5($string);
        $out = '';
        for ($i = 0; $i <= 30; $i = $i + 2) {
            $out .= chr(hexdec(substr($tmp, $i, 2)));
        }

        return $out;
    }
33874
public static function strlenForUtf8($string)
	{
		$l = strlen($string);
		$p = $c = 0;
		while ($p < $l)
		{
			$a = $string[$p];
			if ($a < "\xC0")
				$p++;
			elseif ($a < "\xE0")
				$p += 2;
			elseif ($a < "\xF0")
				$p += 3;
			elseif ($a < "\xF8")
				$p += 4;
			elseif ($a < "\xFC")
				$p += 5;
			else
				$p += 6;
			$c++;
		}
		return $c;
	}
844029
protected static function utf8($string) {
		$map = [];
		$values = [];
		$find = 1;
		$length = strlen($string);
		for ($i = 0; $i < $length; $i++) {
			$value = ord($string[$i]);
			if ($value < 128) {
				$map[] = $value;
			} else {
				if (empty($values)) {
					$find = ($value < 224) ? 2 : 3;
				}
				$values[] = $value;
				if (count($values) === $find) {
					if ($find == 3) {
						$map[] = (($values[0] % 16) * 4096) + (($values[1] % 64) * 64) + ($values[2] % 64);
					} else {
						$map[] = (($values[0] % 32) * 64) + ($values[1] % 64);
					}
					$values = [];
					$find = 1;
				}
			}
		}
		return $map;
	}
893365
public static function checkUtf8Encoding($string)
    {
        if (!mb_check_encoding($string, 'UTF-8') ||
            !$string == mb_convert_encoding(mb_convert_encoding($string, 'UTF-32', 'UTF-8'), 'UTF-8', 'UTF-32')
        ) {
            return false;
        }

        return true;
    }
744964
public function convertToUTF8($string){
			if (is_array($string)) {
				foreach ($string as $k => $v) {
					$string[$k] = $this->convertToUTF8($v);
				}
			} else if (is_string ($string)) {
				return utf8_encode($string);
			}
			return $string;
        }
989087
protected function convertString($string)
    {
        $charset = strtolower($this->getCharset());
        if (!in_array($charset, ['utf-8', 'iso-8859-1', 'iso-8859-15', ''])) {
            return mb_convert_encoding($string, $charset, 'utf-8');
        }

        return $string;
    }
268823
private static function decodeString($string)
    {
        $size = 0;
        $offset = 1;

        // Get some information on the first character.
        list($firstHex, $firstOctal) = self::analyzeFirstChar($string[0]);

        if (in_array(hexdec($firstOctal), array(10, 11))) {
            // fixstr
            $size   = hexdec($firstHex) - bindec('10100000');
            $offset = 1;
        } elseif ($firstHex === 'd9') {
            // str 8
            $size   = hexdec(unpack('H*', substr($string, 1, 1)));
            $offset = 2;
        } elseif ($firstHex === 'da') {
            // str 16
            $size   = hexdec(unpack('H*', substr($string, 1, 2)));
            $offset = 3;
        } elseif ($firstHex === 'db') {
            // str 32
            $size   = hexdec(unpack('H*', substr($string, 1, 4)));
            $offset = 5;
        }

        // Parse off the first $size characters and store it as the data.
        $string = substr($string, $offset);
        $data   = substr($string, 0, $size);
        $string = substr($string, $size);

        // Return
        return array($data, $string);
    }
701517
private static int utf8Length(String string) {
    int stringLength = string.length();
    int utf8Length = 0;
    for (int i = 0; i < stringLength; i++) {
      int c = string.charAt(i);
      if (c <= 0x007F) {
        utf8Length++;
      } else if (c > 0x07FF) {
        utf8Length += 3;
      } else {
        utf8Length += 2;
      }
    }
    return utf8Length;
  }
735754
function _convertString($string)
    {
        // chop away beggining and ending quotes
        $string = substr($string, 1, strlen($string) - 2);
        if (strlen($string) > 255) {
            return $this->raiseError("String is too long");
        }

        if ($this->_BIFF_version == 0x0500) {
            return pack("CC", $this->ptg['ptgStr'], strlen($string)).$string;
        } elseif ($this->_BIFF_version == 0x0600) {
            $encoding = 0;   // TODO: Unicode support
            return pack("CCC", $this->ptg['ptgStr'], strlen($string), $encoding).$string;
        }
    }