• Модуль: main
  • Путь к файлу: ~/bitrix/modules/main/lib/text/encoding.php
  • Класс: BitrixMainTextEncoding
  • Вызов: Encoding::convertByMbstring
static function convertByMbstring($data, $charsetFrom, $charsetTo)
{
	//For UTF-16 we have to detect the order of bytes
	//Default for mbstring extension is Big endian
	//Little endian have to pointed explicitly
	if (strtoupper($charsetFrom) == "UTF-16")
	{
		$ch = substr($data, 0, 1);
		if ($ch == "xFF" && substr($data, 1, 1) == "xFE")
		{
			//If Little endian found - cutoff BOF bytes and point mbstring to this fact explicitly
			$res = mb_convert_encoding(substr($data, 2), $charsetTo, "UTF-16LE");
		}
		elseif ($ch == "xFE" && substr($data, 1, 1) == "xFF")
		{
			//If it is Big endian, just remove BOF bytes
			$res = mb_convert_encoding(substr($data, 2), $charsetTo, $charsetFrom);
		}
		else
		{
			//Otherwise, assime Little endian without BOF
			$res = mb_convert_encoding($data, $charsetTo, "UTF-16LE");
		}
	}
	else
	{
		$res = mb_convert_encoding($data, $charsetTo, $charsetFrom);
	}

	return $res;
}