php emoji utf8转unicode

/**
 * Translates a sequence of UTF-8 bytes to their equivalent unicode code points.
 * Each code point is prefixed with "\u".
 *
 * @param string $utf8
 *
 * @return string
 */function utf8_to_unicode($utf8) {
    $i = 0;
    $l = strlen($utf8);

    $out = ‘‘;

    while ($i < $l) {
        if ((ord($utf8[$i]) & 0x80) === 0x00) {
            // 0xxxxxxx
            $n = ord($utf8[$i++]);
        } elseif ((ord($utf8[$i]) & 0xE0) === 0xC0) {
            // 110xxxxx 10xxxxxx
            $n =
                ((ord($utf8[$i++]) & 0x1F) <<  6) |
                ((ord($utf8[$i++]) & 0x3F) <<  0)
            ;
        } elseif ((ord($utf8[$i]) & 0xF0) === 0xE0) {
            // 1110xxxx 10xxxxxx 10xxxxxx
            $n =
                ((ord($utf8[$i++]) & 0x0F) << 12) |
                ((ord($utf8[$i++]) & 0x3F) <<  6) |
                ((ord($utf8[$i++]) & 0x3F) <<  0)
            ;
        } elseif ((ord($utf8[$i]) & 0xF8) === 0xF0) {
            // 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
            $n =
                ((ord($utf8[$i++]) & 0x07) << 18) |
                ((ord($utf8[$i++]) & 0x3F) << 12) |
                ((ord($utf8[$i++]) & 0x3F) <<  6) |
                ((ord($utf8[$i++]) & 0x3F) <<  0)
            ;
        } elseif ((ord($utf8[$i]) & 0xFC) === 0xF8) {
            // 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
            $n =
                ((ord($utf8[$i++]) & 0x03) << 24) |
                ((ord($utf8[$i++]) & 0x3F) << 18) |
                ((ord($utf8[$i++]) & 0x3F) << 12) |
                ((ord($utf8[$i++]) & 0x3F) <<  6) |
                ((ord($utf8[$i++]) & 0x3F) <<  0)
            ;
        } elseif ((ord($utf8[$i]) & 0xFE) === 0xFC) {
            // 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
            $n =
                ((ord($utf8[$i++]) & 0x01) << 30) |
                ((ord($utf8[$i++]) & 0x3F) << 24) |
                ((ord($utf8[$i++]) & 0x3F) << 18) |
                ((ord($utf8[$i++]) & 0x3F) << 12) |
                ((ord($utf8[$i++]) & 0x3F) <<  6) |
                ((ord($utf8[$i++]) & 0x3F) <<  0)
            ;
        } else {
            throw new \Exception(‘Invalid utf-8 code point‘);
        }

        $n = strtoupper(dechex($n));
        $pad = strlen($n) <= 4 ? strlen($n) + strlen($n) %2 : 0;
        $n = str_pad($n, $pad, "0", STR_PAD_LEFT);

        $out .= sprintf("\u%s", $n);
    }

    return $out;}
时间: 2024-08-10 15:07:18

php emoji utf8转unicode的相关文章

读写UTF-8、Unicode文件(加上了文件头,貌似挺好用)

conf配置文件一些为UTF-8和Unicode格式,这样便可良好的支持多语言,从网上查阅资料后,将读写UTF-8.Unicode文件写了几个最精简的函数,更新后加了是否写文件头的功能,以适应更多需要,注意函数未加防错保护. 参数说明:f文件名.s写入或读取的文件内容.hs文件头.b是否读写文件头. UTF-8文件写入函数 代码 procedure SaveUTF(f:string;s:string;b:boolean=true);var  ms:TMemoryStream;  hs:Strin

UTF-8和Unicode

What's the difference between unicode and utf8? up vote 103 down vote favorite 49 Is it true that unicode=utf16 ? UPDATE Many are saying unicode is a standard not an encoding,but most editors support save as Unicode encoding actually. As Rasmus state

utf-8、unicode 、nsstring转换

一.nsstring转utf-8 NSString * str1=[@"你好" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 二.utf-8转unicode NSString * title=[self utf8ToUnicode:@"你好"]; - (NSString *) utf8ToUnicode:(NSString *)string { NSUInteger length =

paip.utf-8,unicode编码的本质输出unicode文件原理 python

#别的语言,java php都是unicode,走十python不一样. #enddef #todo write to unicode encode fileHandle = open ( r"c:\fmtSmpEnRst.txt", 'w',encoding="UTF-16" ) #todox utf8 not decode...now  UTF-16是Unicode lines=file2list(f, encode="utf-16") 作者

Python中GBK, UTF-8和Unicode的编码问题

编码问题,一直是使用python2时的一块心病.几乎所有的控制台输入输出.IO操作和HTTP操作都会涉及如下的编码问题: UnicodeDecodeError:‘ascii’codec can’t decodebyte0xc4inposition10:ordinalnotinrange(128) 这究竟是是个什么东西?!有时稀里糊涂地用一坨encode(),decode()之类的函数让程序能跑对了,可是下次遇到非ASCII编码时又悲剧了. 那么Python 2.x中的字符串究竟是个什么呢? 基本

(转载)GBK、UTF8、UNICODE编码转换

GBK.UTF8.UNICODE编码转换 1 string GBKToUTF8(const std::string& strGBK) 2 { 3 int nLen = MultiByteToWideChar(CP_ACP, 0, strGBK.c_str(), -1, NULL, 0); 4 WCHAR * wszUTF8 = new WCHAR[nLen]; 5 MultiByteToWideChar(CP_ACP, 0, strGBK.c_str(), -1, wszUTF8, nLen);

libiconv gbk utf-8 转 unicode

#include <stdio.h> #include <string.h> #include <stdlib.h> #include "iconv.h" #define CODE_UNICODE "UCS-2LE" int code_convert(const char *pFromCharset, const char *pToCharset, char *pInBuf, size_t nInLen, char *pOutBu

utf8 to unicode

json utf8 to unicode (stm32 发烧群友提供),仅留做参考,不保证其准确及可用. u32 UTF8_to_Unicode(char *dst, char *src) //json utf8 to unicode { u32 i = 0, unicode = 0, ii, iii; int codeLen = 0; while ( *src ) { //1. UTF-8 ---> Unicode if(0 == (src[0] & 0x80)) { // 单字节 cod

MFC格式转换 UTF8 ANSI UNICODE

函数拿起来就可以用 参数说明:sChartSet : FromANSI(ANSI->UNICODE) , ToANSI (UNICODE->ANSI) , FromUTF8 (UTF8->UNICODE) , ToUTF8 (UNICODE->UTF8) CString CSqlConTestDlg::UnicodeCovert(CString sSourceStr , CString sCharSet) { bool bToUnicode = true; if(!strnicmp