博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
编码转化
阅读量:6901 次
发布时间:2019-06-27

本文共 2869 字,大约阅读时间需要 9 分钟。

hot3.png

/** * utf8转gbk * * @param *        	$utfstr */function utf8_to_gbk($utfstr) {	global $UC2GBTABLE;	$okstr = '';	if (empty ( $UC2GBTABLE )) {		$filename = CODETABLEDIR . 'gb-unicode.table';		$fp = fopen ( $filename, 'rb' );		while ( $l = fgets ( $fp, 15 ) ) {			$UC2GBTABLE [hexdec ( substr ( $l, 7, 6 ) )] = hexdec ( substr ( $l, 0, 6 ) );		}		fclose ( $fp );	}	$okstr = '';	$ulen = strlen ( $utfstr );	for($i = 0; $i < $ulen; $i ++) {		$c = $utfstr [$i];		$cb = decbin ( ord ( $utfstr [$i] ) );		if (strlen ( $cb ) == 8) {			$csize = strpos ( decbin ( ord ( $cb ) ), '0' );			for($j = 0; $j < $csize; $j ++) {				$i ++;				$c .= $utfstr [$i];			}			$c = utf8_to_unicode ( $c );			if (isset ( $UC2GBTABLE [$c] )) {				$c = dechex ( $UC2GBTABLE [$c] + 0x8080 );				$okstr .= chr ( hexdec ( $c [0] . $c [1] ) ) . chr ( hexdec ( $c [2] . $c [3] ) );			} else {				$okstr .= '&#' . $c . ';';			}		} else {			$okstr .= $c;		}	}	$okstr = trim ( $okstr );	return $okstr;}/** * gbk转utf8 * * @param *        	$gbstr */function gbk_to_utf8($gbstr) {	global $CODETABLE;	if (empty ( $CODETABLE )) {		$filename = CODETABLEDIR . 'gb-unicode.table';		$fp = fopen ( $filename, 'rb' );		while ( $l = fgets ( $fp, 15 ) ) {			$CODETABLE [hexdec ( substr ( $l, 0, 6 ) )] = substr ( $l, 7, 6 );		}		fclose ( $fp );	}	$ret = '';	$utf8 = '';	while ( $gbstr ) {		if (ord ( substr ( $gbstr, 0, 1 ) ) > 0x80) {			$thisW = substr ( $gbstr, 0, 2 );			$gbstr = substr ( $gbstr, 2, strlen ( $gbstr ) );			$utf8 = '';			@$utf8 = unicode_to_utf8 ( hexdec ( $CODETABLE [hexdec ( bin2hex ( $thisW ) ) - 0x8080] ) );			if ($utf8 != '') {				for($i = 0; $i < strlen ( $utf8 ); $i += 3)					$ret .= chr ( substr ( $utf8, $i, 3 ) );			}		} else {			$ret .= substr ( $gbstr, 0, 1 );			$gbstr = substr ( $gbstr, 1, strlen ( $gbstr ) );		}	}	return $ret;}/** * unicode转utf8 * * @param *        	$c */function unicode_to_utf8($c) {	$str = '';	if ($c < 0x80) {		$str .= $c;	} elseif ($c < 0x800) {		$str .= (0xC0 | $c >> 6);		$str .= (0x80 | $c & 0x3F);	} elseif ($c < 0x10000) {		$str .= (0xE0 | $c >> 12);		$str .= (0x80 | $c >> 6 & 0x3F);		$str .= (0x80 | $c & 0x3F);	} elseif ($c < 0x200000) {		$str .= (0xF0 | $c >> 18);		$str .= (0x80 | $c >> 12 & 0x3F);		$str .= (0x80 | $c >> 6 & 0x3F);		$str .= (0x80 | $c & 0x3F);	}	return $str;}/** * utf8转unicode * * @param *        	$c */function utf8_to_unicode($c) {	switch (strlen ( $c )) {		case 1 :			return ord ( $c );		case 2 :			$n = (ord ( $c [0] ) & 0x3f) << 6;			$n += ord ( $c [1] ) & 0x3f;			return $n;		case 3 :			$n = (ord ( $c [0] ) & 0x1f) << 12;			$n += (ord ( $c [1] ) & 0x3f) << 6;			$n += ord ( $c [2] ) & 0x3f;			return $n;		case 4 :			$n = (ord ( $c [0] ) & 0x0f) << 18;			$n += (ord ( $c [1] ) & 0x3f) << 12;			$n += (ord ( $c [2] ) & 0x3f) << 6;			$n += ord ( $c [3] ) & 0x3f;			return $n;	}}

转载于:https://my.oschina.net/u/3054838/blog/3039755

你可能感兴趣的文章
The Infinite Loop belong to loop statement
查看>>
聊天室
查看>>
慢慢积累 ---------- unity 碰撞 和 碰撞之后
查看>>
5.Struts2-Struts标签
查看>>
记一次修炼路上的JDBC链接数据库的案例
查看>>
两个栈实现一个队列
查看>>
数据库分页
查看>>
ehcache的介绍和使用
查看>>
Android利用Gson解析嵌套多层的Json
查看>>
log4j.properties配置详解与实例
查看>>
Spring学习笔记:Spring概述,第一个IoC依赖注入案例
查看>>
let和var的区别
查看>>
《面向模式的软件体系结构1--模式系统》读书笔记(9)--- 模式系统
查看>>
2012年11月8日学习研究报告
查看>>
《黑客与画家》读后感
查看>>
jq中html(),text(),val()以及js中innerHTML,innerText和value
查看>>
**RESTful API版本控制策略
查看>>
win7更改配色方案介绍~
查看>>
CImageList用法介绍
查看>>
[LeetCode] 40. Combination Sum II
查看>>