• Модуль: mail
  • Путь к файлу: ~/bitrix/modules/mail/classes/general/mail.php
  • Класс: CAllMailUtil
  • Вызов: CAllMailUtil::convertCharset
static function convertCharset($str, $from, $to)
{
	if (!trim($str))
		return $str;

	$from = trim(mb_strtolower($from));
	$to   = trim(mb_strtolower($to));

	$escape = function ($matches)
	{
		return isset($matches[2]) ? '?' : $matches[1];
	};

	if ($from != $to)
	{
		if (in_array($from, array('utf-8', 'utf8')))
		{
			// escape all invalid (rfc-3629) utf-8 characters
			$str = preg_replace_callback('/
				([x00-x7F]+
					|[xC2-xDF][x80-xBF]
					|xE0[xA0-xBF][x80-xBF]|[xE1-xECxEExEF][x80-xBF]{2}|xED[x80-x9F][x80-xBF]
					|xF0[x90-xBF][x80-xBF]{2}|[xF1-xF3][x80-xBF]{3}|xF4[x80-x8F][x80-xBF]{2})
				|([x80-xFF])
			/x', $escape, $str);
		}

		if ($result = BitrixMainTextEncoding::convertEncoding($str, $from, $to))
			$str = $result;
		else
			addMessage2Log(sprintf('Failed to convert email part. (%s -> %s : %s)', $from, $to, $error));
	}

	if (in_array($to, array('utf-8', 'utf8')))
	{
		// escape invalid (rfc-3629) and 4-bytes utf-8 characters
		$str = preg_replace_callback('/
			([x00-x7F]+
				|[xC2-xDF][x80-xBF]
				|xE0[xA0-xBF][x80-xBF]|[xE1-xECxEExEF][x80-xBF]{2}|xED[x80-x9F][x80-xBF])
			|([x80-xFF])
		/x', $escape, $str);
	}

	return $str;
}