PHP中的UNICODE 編碼與解碼_PHP教程
推薦:PHP 過濾頁面中的BOM(實(shí)現(xiàn)代碼)本篇文章是對(duì)PHP中過濾頁面中的BOM的實(shí)現(xiàn)代碼進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下 復(fù)制代碼 代碼如下: function checkBOM ($filename) { $contents = file_get_contents($filename); $charset[1] = substr($contents, 0, 1); $charset[2] = substr($contents, 1, 1)
本篇文章是對(duì)PHP中的UNICODE 編碼與解碼進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下方法一:
<?php
function unicode_encode($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)
{ //兩個(gè)字節(jié)的文字
$str .= 'u'.base_convert(ord($c), 10, 16).str_pad(base_convert(ord($c2), 10, 16), 2, 0, STR_PAD_LEFT);
}
else
{
$str .= $c2;
}
}
return $str;
}
//將UNICODE編碼后的內(nèi)容進(jìn)行解碼
function unicode_decode($name)
{
//轉(zhuǎn)換編碼,將Unicode編碼轉(zhuǎn)換成可以瀏覽的utf-8編碼
$pattern = '/([w]+)|(\u([w]{4}))/i';
preg_match_all($pattern, $name, $matches);
if (!empty($matches))
{
$name = '';
for ($j = 0; $j < count($matches[0]); $j++)
{
$str = $matches[0][$j];
if (strpos($str, '\u') === 0)
{
$code = base_convert(substr($str, 2, 2), 16, 10);
$code2 = base_convert(substr($str, 4), 16, 10);
$c = chr($code).chr($code2);
$c = iconv('UCS-2', 'UTF-8', $c);
$name .= $c;
}
else
{
$name .= $str;
}
}
}
return $name;
}
方法二:
function unicode2utf8($str){
if(!$str) return $str;
$decode = json_decode($str);
if($decode) return $decode;
$str = '["' . $str . '"]';
$decode = json_decode($str);
if(count($decode) == 1){
return $decode[0];
}
return $str;
}
分享:解析PHP的session過期設(shè)置網(wǎng)上很多人給出了解答:修改php配置文件中的session.gc_maxlifetime。如果想了解更多session回收機(jī)制,繼續(xù)閱讀。(本文環(huán)境php5.2) 概述: 每 一次php請(qǐng)求,會(huì)有1/100的概率(默認(rèn)值)觸發(fā)session回收。如果session回收發(fā)生,那就會(huì)檢查/tmp/sess_*的文 件,如果最后
- PHPNOW安裝Memcached擴(kuò)展方法詳解
- php記錄頁面代碼執(zhí)行時(shí)間
- PHP中獎(jiǎng)概率的抽獎(jiǎng)算法程序代碼
- apache設(shè)置靜態(tài)文件緩存方法介紹
- php對(duì)圖像的各種處理函數(shù)代碼小結(jié)
- PHP 關(guān)于訪問控制的和運(yùn)算符優(yōu)先級(jí)介紹
- 關(guān)于PHP語言構(gòu)造器介紹
- php/js獲取客戶端mac地址的實(shí)現(xiàn)代碼
- php5.5新數(shù)組函數(shù)array_column使用
- PHP preg_match的匹配多國(guó)語言的技巧
- php 中序列化和json使用介紹
- php采集文章中的圖片獲取替換到本地
PHP教程Rss訂閱編程教程搜索
PHP教程推薦
- PHP上傳自動(dòng)生成縮略圖及水印類(含代碼)
- 使用PHP獲取當(dāng)前url路徑的函數(shù)以及服務(wù)器變量
- 淺析Dos下運(yùn)行php.exe,出現(xiàn)沒有找到php_mbstring.dll 錯(cuò)誤的解決方法
- 揭秘學(xué)習(xí)php的八個(gè)問題和解決方法
- 解析PHP如何輸出簡(jiǎn)單動(dòng)態(tài)WAP頁面
- 克隆一個(gè)新項(xiàng)目的快捷方式
- 實(shí)用:動(dòng)態(tài)網(wǎng)頁制作技術(shù)PHP的十個(gè)應(yīng)用技巧
- 十天學(xué)會(huì)php之第七天
- 解答PHP上傳多個(gè)圖片并校驗(yàn)的代碼問題
- php設(shè)計(jì)模式之觀察者模式的應(yīng)用詳解
- 相關(guān)鏈接:
- 教程說明:
PHP教程-PHP中的UNICODE 編碼與解碼
。