王新阳

wangxinyang

UTF-8、UNICODE互转

<?php
//UTF-8转UNICODE
function utf8_unicode($s)
{
	if (is_string($s)) {
		$s = mb_str_split($s);
	}
	$arr = array();
	$n = 0;
	for ($i = 0; $i < count($s); $i++) {
		switch (strlen($s[$i])) {
			case 1:
				$n = ord($s[$i]);
				break;
			case 2:
				$n = (ord($s[$i][0]) & 63) << 6;
				$n += ord($s[$i][1]) & 63;
				break;
			case 3:
				$n = (ord($s[$i][0]) & 31) << 12;
				$n += (ord($s[$i][1]) & 63) << 6;
				$n += ord($s[$i][2]) & 63;
				break;
			case 4:
				$n = (ord($s[$i][0]) & 15) << 18;
				$n += (ord($s[$i][1]) & 63) << 12;
				$n += (ord($s[$i][2]) & 63) << 6;
				$n += ord($s[$i][3]) & 63;
				break;
		}
		$arr[] = $n;
	}
	return $arr;
}
function mb_str_split($str, $charset = 'utf-8', $length = 1)
{
	if ($length < 1) {
		return FALSE;
	}
	$result = array();
	for ($i = 0; $i < mb_strlen($str, $charset); $i += $length) {
		$result[] = mb_substr($str, $i, $length, $charset);
	}
	return $result;
}
//UNICODE转UTF-8
function unicode_utf8($u)
{
	if (is_int($u)) {
		$u = array($u);
	}
	$arr = array();
	$tmp = '';
	for ($i = 0; $i < count($u); $i++) {
		if ($u[$i] < 128) {
			$tmp = $u[$i];
		} else {
			if ($u[$i] < 2048) {
				$tmp = chr(192 | $u[$i] >> 6);
				$tmp .= chr(128 | $u[$i] & 63);
			} else {
				if ($u[$i] < 65536) {
					$tmp = chr(224 | $u[$i] >> 12);
					$tmp .= chr(128 | $u[$i] >> 6 & 63);
					$tmp .= chr(128 | $u[$i] & 63);
				} else {
					if ($u[$i] < 2097152) {
						$tmp = chr(240 | $u[$i] >> 18);
						$tmp .= chr(128 | $u[$i] >> 12 & 63);
						$tmp .= chr(128 | $u[$i] >> 6 & 63);
						$tmp .= chr(128 | $u[$i] & 63);
					}
				}
			}
		}
		$arr[] = $tmp;
	}
	return $arr;
}
2015-12-30
2024-11-23 星期六 农历十月二十三