日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > php >内容正文

php

php detect unicode,php-functions/unicode.php at master · xiilei/php-functions · GitHub

發布時間:2023/12/19 php 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。