php utf-8轉(zhuǎn)unicode的函數(shù)
更新時間:2008年06月17日 19:01:35 作者:
php下我們想把uft-8,轉(zhuǎn)成unicode可以用下面的函數(shù)來實現(xiàn)
c#實現(xiàn)代碼
/**
* utf-8 轉(zhuǎn)換成 unicode
* @author fanhui
* 2007-3-15
* @param inStr
* @return
*/
public static String utf8ToUnicode(String inStr) {
char[] myBuffer = inStr.toCharArray();
StringBuffer sb = new StringBuffer();
for (int i = 0; i < inStr.length(); i++) {
UnicodeBlock ub = UnicodeBlock.of(myBuffer[i]);
if(ub == UnicodeBlock.BASIC_LATIN){
//英文及數(shù)字等
sb.append(myBuffer[i]);
}else if(ub == UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS){
//全角半角字符
int j = (int) myBuffer[i] - 65248;
sb.append((char)j);
}else{
//漢字
short s = (short) myBuffer[i];
String hexS = Integer.toHexString(s);
String unicode = "\\u"+hexS;
sb.append(unicode.toLowerCase());
}
}
return sb.toString();
}
/**
* unicode 轉(zhuǎn)換成 utf-8
* @author fanhui
* 2007-3-15
* @param theString
* @return
*/
public static String unicodeToUtf8(String theString) {
char aChar;
int len = theString.length();
StringBuffer outBuffer = new StringBuffer(len);
for (int x = 0; x < len;) {
aChar = theString.charAt(x++);
if (aChar == '\\') {
aChar = theString.charAt(x++);
if (aChar == 'u') {
// Read the xxxx
int value = 0;
for (int i = 0; i < 4; i++) {
aChar = theString.charAt(x++);
switch (aChar) {
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
value = (value << 4) + aChar - '0';
break;
case 'a':
case 'b':
case 'c':
case 'd':
case 'e':
case 'f':
value = (value << 4) + 10 + aChar - 'a';
break;
case 'A':
case 'B':
case 'C':
case 'D':
case 'E':
case 'F':
value = (value << 4) + 10 + aChar - 'A';
break;
default:
throw new IllegalArgumentException(
"Malformed \\uxxxx encoding.");
}
}
outBuffer.append((char) value);
} else {
if (aChar == 't')
aChar = '\t';
else if (aChar == 'r')
aChar = '\r';
else if (aChar == 'n')
aChar = '\n';
else if (aChar == 'f')
aChar = '\f';
outBuffer.append(aChar);
}
} else
outBuffer.append(aChar);
}
return outBuffer.toString();
}
您可能感興趣的文章:
- PHP通過iconv將字符串從GBK轉(zhuǎn)換為UTF8字符集
- PHP如何實現(xiàn)Unicode和Utf-8編碼相互轉(zhuǎn)換
- 支持生僻字且自動識別utf-8編碼的php漢字轉(zhuǎn)拼音類
- PHP UTF8編碼內(nèi)的繁簡轉(zhuǎn)換類
- 用PHP實現(xiàn)將GB編碼轉(zhuǎn)換為UTF8
- 用PHP將Unicode 轉(zhuǎn)化為UTF-8的實現(xiàn)方法(推薦)
- PHP實現(xiàn)十進制、二進制、八進制和十六進制轉(zhuǎn)換相關(guān)函數(shù)用法分析
- PHP二進制與字符串之間的相互轉(zhuǎn)換教程
- PHP函數(shù)篇詳解十進制、二進制、八進制和十六進制轉(zhuǎn)換函數(shù)說明
- PHP中實現(xiàn)中文字符進制轉(zhuǎn)換原理分析
- PHP實現(xiàn)UTF8二進制及明文字符串的轉(zhuǎn)化功能示例
相關(guān)文章
php中如何同時使用session和cookie來保存用戶登錄信息
本篇文章是對在php中同時使用session和cookie來保存用戶登錄信息的實現(xiàn)代碼進行了詳細的分析介紹,需要的朋友參考下2013-07-07
require(),include(),require_once()和include_once()區(qū)別
面試中最容易提到的一個PHP的問題,我想和大家共勉一下: require()和include()有許多相似之處,也有些不同。理解它們的不同點非常重要,否則很容易犯錯誤。2008-03-03

