本文目錄一覽:
php中編碼轉換問題
function uc2html($str) {
$ret = ‘ ‘;
for( $i=0; $i strlen($str)/2; $i++ ) {
$charcode = ord($str[$i*2])+256*ord($str[$i*2+1]);
$ret .= iconv( “utf-8 “, “gb2312 “,u2utf8($charcode));
}
return $ret;
}
function u2utf8($c) {
$str= ” “;
if ($c 0x80) {
$str.=$c;
} else if ($c 0x800) {
$str.=chr(0xC0 | $c 6);
$str.=chr(0x80 | $c 0x3F);
} else if ($c 0x10000) {
$str.=chr(0xE0 | $c 12);
$str.=chr(0x80 | $c 6 0x3F);
$str.=chr(0x80 | $c 0x3F);
} else if ($c 0x200000) {
$str.=chr(0xF0 | $c 18);
$str.=chr(0x80 | $c 12 0x3F);
$str.=chr(0x80 | $c 6 0x3F);
$str.=chr(0x80 | $c 0x3F);
}
return $str;
}
如果你不是smarty的話 試試這個 如果是smarty的話 用下面的方法
?php
/*
@Author: 蝸牛
@Blog:
@Note: 這個解決辦法是基於上面那個地址提到的方法,解決了中英文截取長度時出現亂碼的問題
*/
function smarty_modifier_truncate($string, $sublen = 80, $etc = ‘…’, $break_words = false, $middle = false)
{
$start=0;
$code=”UTF-8″;
if($code == ‘UTF-8’)
{
//如果有中文則減去中文的個數
$cncount=cncount($string);
if($cncount($sublen/2))
{
$sublen=ceil($sublen/2);
}
else
{
$sublen=$sublen-$cncount;
}
$pa = “/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xef][\x80-\xbf][\x80-\xbf]|\xf0[\x90-\xbf][\x80-\xbf][\x80-\xbf]|[\xf1-\xf7][\x80-\xbf][\x80-\xbf][\x80-\xbf]/”;
preg_match_all($pa, $string, $t_string);
if(count($t_string[0]) – $start $sublen) return join(”, array_slice($t_string[0], $start, $sublen)).”…”;
return join(”, array_slice($t_string[0], $start, $sublen));
}
else
{
$start = $start*2;
$sublen = $sublen*2;
$strlen = strlen($string);
$tmpstr = ”;
for($i=0; $i$strlen; $i++)
{
if($i=$start $i($start+$sublen))
{
if(ord(substr($string, $i, 1))129)
{
$tmpstr.= substr($string, $i, 2);
}
else
{
$tmpstr.= substr($string, $i, 1);
}
}
if(ord(substr($string, $i, 1))129) $i++;
}
if(strlen($tmpstr)$strlen ) $tmpstr.= “…”;
return $tmpstr;
}
}
function cncount($str)
{
$len=strlen($str);
$cncount=0;
for($i=0;$i$len;$i++)
{
$temp_str=substr($str,$i,1);
if(ord($temp_str) 127)
{
$cncount++;
}
}
return ceil($cncount/3);
}
?
是可以的以上兩種方法 site:
淺析PHP中的字元串編碼轉換(自動識別原編碼)
本篇文章是對PHP中字元串編碼轉換的實現代碼進行了詳細的分析介紹,需要的朋友參考下
複製代碼
代碼如下:
/**
*
對數據進行編碼轉換
*
@param
array/string
$data
數組
*
@param
string
$output
轉換後的編碼
*/
function
array_iconv($data,$output
=
‘utf-8’)
{
$encode_arr
=
array(‘UTF-8′,’ASCII’,’GBK’,’GB2312′,’BIG5′,’JIS’,’eucjp-win’,’sjis-win’,’EUC-JP’);
$encoded
=
mb_detect_encoding($data,
$encode_arr);//自動判斷編碼
if
(!is_array($data))
{
return
mb_convert_encoding($data,
$output,
$encoded);
}
else
{
foreach
($data
as
$key=$val)
{
if(is_array($val))
{
$data[$key]
=
array_iconv($val,
$input,
$output);
}
else
{
$data[$key]
=
mb_convert_encoding($data,
$output,
$encoded);
}
}
return
$data;
}
}
php 編碼轉換
URLEncode:是指針對網頁url中的中文字元的一種編碼轉化方式,最常見的就是Baidu、Google等搜索引擎中輸入中文查詢時候,生成經過Encode過的網頁URL。
URLEncode的方式一般有兩種,一種是傳統的基於GB2312的Encode(Baidu、Yisou等使用),另一種是基於UTF-8的Encode(Google、Yahoo等使用)。
本工具分別實現兩種方式的Encode與Decode:
中文 – GB2312的Encode – %D6%D0%CE%C4
中文 – UTF-8的Encode – %E4%B8%AD%E6%96%87
我們可以用以下代碼實現轉換:
?php echo urlencode(‘測試’);?
如果是gb2312編碼,轉換的結果為”%B2%E2%CA%D4″;
如果是utf-8編碼,轉換的結果為”%E6%B5%8B%E8%AF%95″;
希望我的回答你能滿意啊!呵呵!
PHP 解決utf-8和gb2312編碼轉換問題
終於皇天不負有心人,答案還是讓我找到了。
網上的都是這樣用的
複製代碼
代碼如下:
$content
=
iconv(“utf-8″,”gb2312”,$content);
這樣做其實也對著了,看著確實是把utf-8轉化為gb2312了,但是實際運行的話,往往都是以失敗告終的,原因呢?
原因實際上也很簡單,因為任何的函數都是執行錯誤的時候,同時很不幸的是iconv();就很終於出現錯誤。現在給你正確的答案。
真正的答案是這樣的
複製代碼
代碼如下:
$content
=
iconv(“utf-8″,”gb2312//IGNORE”,$content);
很簡單的,只要後面加上一個//IGNORE就行,加上這個就可以是ICONV()函數忽略錯誤,繼續執行。
同理,要像把gb2312換為utf-8隻要寫上$content
=
iconv(“gb2312″,”utf-8//IGNORE”,$content);就行
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/277422.html