php detect unicode,php-functions/unicode.php at master · xiilei/php-functions · GitHub
/*
DOC
@@ 字符编码转换:
iconv:
document: http://cn2.php.net/manual/zh/function.iconv.php
code:
// 把UTF-8的编码转换为 UCS-4 的编码
iconv('UTF-8', 'UCS-4', $string);
mb_convert_encoding:
document: http://cn2.php.net/manual/zh/function.mb-convert-encoding.php
code:
// 把UTF-8的编码转换为 UCS-4 的编码
mb_convert_encoding($string, 'ucs-4', 'utf-8');
@@ 进制转换
bin2hex:
document: http://cn2.php.net/manual/zh/function.bin2hex.php
code:
// 把 UCS-4 字符 "我" 转换成 十六进制表示 00004f60
$ucs4_string = mb_convert_encoding('你', 'ucs-4', 'utf-8');
echo bin2hex($ucs4_string); // 00004f60
hex2bin:
document: http://cn2.php.net/manual/zh/function.hex2bin.php
code:
// 将十六进制字符串转换为二进制字符串
$ucs4_string = mb_convert_encoding('你', 'ucs-4', 'utf-8');
$hex_string = bin2hex($ucs4_string); // 00004f60
$bin_string = hex2bin($hex_string);
// 把 ucs-4 编码转换为 utf-8 编码
$utf8_string = mb_convert_encoding($bin_string , 'utf-8', 'ucs-4' );
base_convert:
// 在任意进制之间转换数字
document: http://cn2.php.net/manual/zh/function.base-convert.php
code:
$ucs4_string = mb_convert_encoding('你', 'ucs-4', 'utf-8');
$hex_string = bin2hex($ucs4_string); // 00004f60
$dec_string = base_convert($hex_string, 16, 10); // 将字符串的十六进制数字转换为 十进制
// XML 实体字符就是 UTF-8 字符串的十进制表示
// 加上 起始符''然后加上结尾符';'
// 比如: ⭧ ( 其中 11111 即为 utf-8 字符的十进制表示 )
// 由于我们当前的字符编码已被转换为 UCS-4编码,所以如果需要组装成 XML 实体字符,还需要进行编码转换
$ucs4_string = hex2_bin(base_convert($dec_string, 10, 16)); // UCS-4 字符串
$utf8_string = mb_convert_encoding($ucs4_string, 'utf-8', 'ucs-4');
$hex_string = bin2hex($utf8_string);
$dec_string = base_convert($hex_string, 16, 10);
$xml_string = "{$dec_string};"; // XML 实体字符
@ 其它替代函数
pack:
document: http://cn2.php.net/manual/zh/function.pack.php
desc: 强烈建议大家需要阅读该函数
该函数在大部分正常语言都是一个及其重要的基础函数
PHP 的 bin2hex 和 hex2bin 以及 base_convert 不过是对该 pack 函数做了一个 简单封装而已
类似与 Python 的 binascii Lib(当然功能不能类比....).
Recode:
document: http://cn2.php.net/manual/zh/book.recode.php
desc : GNU Recode ( 字符编码判断以及互换 )
@ 关于字符集的简单介绍
UTF-32:
document: http://zh.wikipedia.org/zh-cn/UTF-32
desc : UTF-32 是 UCS-4 的一个子集
UTF-16:
document: http://zh.wikipedia.org/wiki/UTF-16
desc : UTF-16 是 UCS-2 的一个子集
####################################################
## 关于字符集及编码方面的更多问题,如有描述不正确的地方,欢迎指正。
## 电邮: gnulinux@126.com
####################################################
*/
class Ustring{
/*
Require Package(EXT):
iconv: http://cn2.php.net/manual/zh/book.iconv.php
mbstring: http://cn2.php.net/manual/zh/ref.mbstring.php
*/
//static $CODE
public function __construct ($string=''){
iconv_set_encoding("internal_encoding", "UTF-8"); // http://cn2.php.net/manual/zh/function.iconv-set-encoding.php
$encoding = array('UTF-8') ;
$is_utf8 = mb_check_encoding ( $string, $encoding ) ; // http://cn2.php.net/manual/zh/function.mb-check-encoding.php
if ( !$is_utf8 ){
return false;
}
//self::$string = $string;
}
public function encode($encoding){
// 编码字符串
//mb_convert_encoding($string, 'ucs-4', 'utf-8');
}
public function decode($encoding){
// 解码字符串
}
public function detect(){
}
public function toUTF8(){
}
public function toUCS4(){
}
public function toUCS2(){
}
public function toGB18030(){
}
public function toXML($string){
// XML 实体字符
$ucs4 = bin2hex(mb_convert_encoding($string, 'ucs-4', 'utf-8')); // 每个字符长度为8个字节(十六进制)
$len = strlen($ucs4);
$xml_str = '';
for ($i=0; $i
$s = substr($ucs4, $i, 8);
$xml_str .= ''.base_convert($s, 16, 10).';'; // 十六进制 转换为 十进制表示
}
return $xml_str;
}
public function toList($string){
// Safe List.
$ucs4 = bin2hex(mb_convert_encoding($string, 'ucs-4', 'utf-8')); // 每个字符长度为8个字节(十六进制)
$len = strlen($ucs4);
$str_array = array();
for ($i=0; $i
$ucs4_str = substr($ucs4, $i, 8);
$utf8_str = mb_convert_encoding(hex2bin($ucs4_str), 'utf-8', 'ucs-4');
$str_array[] = $utf8_str;
}
return $str_array;
}
}
function test_XMLString(){
// 测试 转换 XML 实体字符
$us = new Ustring();
$string = "你好,World.";
echo $us->toXML($string);
echo "\n";
}
function test_Str2List(){
// Safe List.
$us = new Ustring();
$string = "你好,World.";
print_r($us->toList($string) );
echo "\n";
}
// test_XMLString(); // 测试 字符串转化为 XML 的十进制实体字符
// test_Str2List(); // 测试字符串转换为列表 ( 多维查找需要用到 )
/*
Old test function.
function utf8_unicode($name){
$name = iconv('UTF-8', 'UCS-2', $name);
$len = strlen($name);
$str = '';
for ($i = 0; $i < $len - 1; $i = $i + 2){
$c = $name[$i];
$c2 = $name[$i + 1];
if (ord($c) > 0){ //两个字节的文字
$str .= '\u'.base_convert(ord($c), 10, 16).str_pad(base_convert(ord($c2), 10, 16), 2, 0, STR_PAD_LEFT);
//$str .= base_convert(ord($c), 10, 16).str_pad(base_convert(ord($c2), 10, 16), 2, 0, STR_PAD_LEFT);
} else {
$str .= '\u'.str_pad(base_convert(ord($c2), 10, 16), 4, 0, STR_PAD_LEFT);
//$str .= str_pad(base_convert(ord($c2), 10, 16), 4, 0, STR_PAD_LEFT);
}
}
$str = strtoupper($str);//转换为大写
return $str;
}
function utf8_usc4($str){
// 从 UTF-8 编码 转换至 UCS-4 ( UTF-32子集 ) 编码
$ucs4 = bin2hex(mb_convert_encoding($str, 'ucs-4', 'utf-8')); // 每个字符长度为8个字节(十六进制)
$len = strlen($ucs4);
$str_array = array(); // 字符序列
for ($i=0;$i
$s = substr($ucs4, $i, 8); // 提取八个字节的比特
$aaa = hex2bin($s);
$bbb = iconv('UCS-4', 'UTF-8', $aaa);
echo $bbb;
echo "\t";
$s_number = base_convert($s, 16, 10); // 计算该八字节长度字符的十进制数字( 从十六进制转换过来 )
$str_array[] = $s_number; // 追加进 字符序列
}
return $str_array;
}
echo bin2hex(mb_convert_encoding('你', 'ucs-4', 'utf-8'));
echo "\n";
$a = utf8_usc4('你好.nihao。');
print_r($a);
$b = base_convert(22909, 10, 16) ;
echo hex2bin($b)."\n";
$bb = mb_convert_encoding( hex2bin($b), 'utf-8', 'ucs-4' );
var_dump($bb);
//utf8_unicode("你好,nihao.");
// UCS-4 : UTF-32 (http://zh.wikipedia.org/wiki/UTF-32)
// iconv参考:http://www.php.net/manual/zh/function.iconv.php
// $str = "你";
// $ucs2 = iconv('UTF-8', 'UCS-4', $str);
// echo strlen($ucs2)."\n";exit();
// $hex= bin2hex($ucs2);
// echo $hex."\n";
// $unicode_html = ''.base_convert($hex, 16, 10).';';
// echo $unicode_html;
function xml_code($str){
//$ucs4 = iconv('UTF-8', 'UCS-4', $str); // 长度为4个字节
// 支持多字节字符(其实只要脚本编码统一,多字节字符可以不需要判断)
$ucs4 = bin2hex(mb_convert_encoding($str, 'ucs-4', 'utf-8')); // 每个字符长度为8个字节(十六进制)
$len = strlen($ucs4);
$xml_str = '';
for ($i=0;$i
$s = substr($ucs4,$i,8);
$xml_str .= ''.base_convert($s,16,10).';';
}
return $xml_str;
}
function other(){
// 参考:http://blog.longwin.com.tw/2011/06/php-html-unicode-convert-2011/
$str = '我';
將 '我' 轉換成 '25105' 或 '我'
// 使用 iconv
$unicode_html = base_convert(bin2hex(iconv('UTF-8', 'UCS-4', $str)), 16, 10); // 25105
// 使用 mb_convert_encoding
$unicode_html = base_convert(bin2hex(mb_convert_encoding($str, 'ucs-4', 'utf-8')), 16, 10); // 25105
// 补上 xxxx;
$unicode_html = '' . base_convert(bin2hex(iconv("utf-8", "ucs-4", $str)), 16, 10) . ';'; // 我
// 将 我 转回 '我'
$str = mb_convert_encoding($unicode_html, 'UTF-8', 'HTML-ENTITIES'); // '我', $unicode_html = '我'
}
//echo xml_code("你好恩asd啊数据库的发生空间的阿斯顿吧数据库");
*/
php detect unicode,php-functions/unicode.php at master · xiilei/php-functions · GitHub相关推荐
- VB 文件编码互换模块(支持 Ansi,UTF-8,Unicode(little endian),Unicode big endian)
'VB 文件编码互换模块,支持对Ansi,UTF-8,Unicode(little endian),Unicode big endian编码之间进行转换. Option Explicit Privat ...
- pycocotools报错 if type(resFile) == str or type(resFile) == unicode: NameError: name ‘unicode‘ is
Q: pycocotools报错 if type(resFile) == str or type(resFile) == unicode: NameError: name 'unicode' is n ...
- VC++ 工程添加 Unicode Debug 和 Unicode Release编译支持
原文地址: 学习unicode 前言 昨天为了编译一个网上下载程序,下载vs2008,转换工程并加入自己的部分程序,最后还是运行不了.郁闷之余,查看错误,发现原来自己的代码有问题.比如vs2008不再 ...
- python的unicode编码_python unicode编码
遇到编码问题,查阅了一些资料,有了一些理解,简单记录下. 首先,Unicode有个通用字符集 其次,每个字符有个编号(编码,即code points),规范为U+hhhh,其中每个h代表一个十六进制数 ...
- Unicode介绍及Unicode编程
目录 1.什么是Unicode? 2.为什么使用Unicode? 3.Unicode有什么缺点 4.Unicode编程 4.1 C运行时库对Unicode的支持 4.1.1 字符串类型 4.1.2 字 ...
- Unicode研究之 Unicode 13介绍
Unicode研究之 Unicode 13介绍 发布时间 2020年3月10日(公告) 总结 Unicode 13.0添加了5,930个字符,总共143,859个字符.这些新增功能包括4个新脚本,总共 ...
- unicode,ansi,utf-8,unicode big endian编码的区别
为什么80%的码农都做不了架构师?>>> 随便说说字符集和编码 快下班时,爱问问题的小朋友Nico又问了一个问题: "sqlserver里面有char和nchar,那 ...
- java对unicode转码,Unicode编码和中文互转(JAVA实现)
//中文转Unicode public static String gbEncoding(final String gbString) { //gbString = "测试 ...
- 为vc工程添加Unicode Debug和Unicode Release
通过使用unicode编译,软件可以适应多种情况,如何在自己的工程中添加这两种编译方式呢?下面是一个简单的步骤 1.新建一个工程: 2.选择"Build->Confi ...
最新文章
- Android Studio中RecycerView依赖库加载问题
- rrdtool的完整例子
- java怎么进行静态引用_java – 如何解决“无法对非静态字段或方法进行静态引用”?...
- numpy随机生成01矩阵_NumPy数组基本介绍
- 在angular中一个页面滚动后,打开新页面不在最顶部的解决办法
- linux通过tar包安装docker
- vue 限制输入字符长度
- DDGScreenShot —图片加各种滤镜高逼格操作
- 【扯淡】今天看了《写给失眠者的心理学》,下面开始借鉴与脑洞大开
- JavaScript学习笔记(3)——JavaScript与HTML的组合方式
- 手把手教你用JAVA调用Websocket实现“声音转换”功能(变声)标贝科技
- 尚硅谷前端框架vue语法(二)
- 计算机机房装修效果图,机房装修施工流程是什么? 机房装修效果图
- 学生课程成绩信息实体表设计mysql_数据库综合实验--设计某高校学生选课管理系统...
- 对接支付宝单笔转账接口
- 经典数字图像处理素材库
- 为什么人生下来就有意识 人脑五大未解之谜
- 大疆Phantom 4 RTK 通过4G SIM卡来控制飞机
- Error(3, 32) java 无法访问org.springframework.boot.SpringApplication
- cadence SPB17.4 - allegro - create and switch Visibility view
热门文章
- SAP Spartacus后台CMS Component和Angular Component的映射关系
- 如何自定义SAP Spartacus 产品明细的url pattern
- SAP CDS view自学教程之十:SAP CDS view扩展性(Extensibility)实现原理
- 在SAP HANA Express Edition里创建数据库表
- 利用Object.defineProperty挂接set钩子,监控对象属性的修改事件
- 纯JavaScript实现的调用设备摄像头并拍照的功能
- BSP application view instance lifetime analysis
- 如何解决error message Data cannot be maintained for set type COM_TA_R3_ID
- One order datatype 命名规范
- UI component html code 查看工具