擷取處理UTF 8編碼字串

2021-10-25 09:23:38 字數 709 閱讀 8762

/**

* 擷取處理utf-8編碼字串

* 規則:中文2個字元,數字、英文1個字元;擷取末尾不足乙個漢字的則捨棄。

* @param string $str

* @param int $len 截取出的字元長度

* @author flyer0126

* @since 2012/05/03

*/function substr_utf8_cn($str, $len)

$result_str = '';

for($i=0;$i<$len;$i++)

$i++;

} else

}return $result_str;

}//useage:

$str1 = '擷取處理字串測試內容';

$str2 = '11aa22bb擷取處理字串測試內容';

$str3 = '#$%^擷取處理字串測試內容';

echo substr_utf8_cn($str1, 9, '...'),php_eol;

echo substr_utf8_cn($str2, 9),php_eol;

echo substr_utf8_cn($str3, 9);

//outputs:

/**擷取處理

11aa22bb

#$%^擷取

**/

字串轉utf8編碼

url字串中存在中文,需要把中文轉換成utf8編碼,我所知道的四種方法 uri.escapeuristring uri.escapedatastring httputility.urlencode,www.escapeurl httputility.urlencode存在system.web中,需要...

Python2未知編碼字串轉為utf 8編碼

python的編碼問題總是非常煩人,一不小心就出來亂碼,本函式採用的方法為 分別用try判斷是否能解碼成功,如果能成功表明為相應編碼,解碼成功後再編碼為utf 8,如下 coding utf 8 defto utf8 data try print trying decode with utf 8 d...

C 實現utf8和gbk編碼字串互相轉換

不同系統或者伺服器之間訊息傳遞經常遇到編碼轉換問題,這裡用c 實現了乙個輕量的gbk和utf8互相轉換,可跨平台使用。重量級的可以用libiconv庫 在windows下用標頭檔案裡的函式進行多位元組和寬字元轉換,linux下採用標頭檔案裡的函式進行編碼互相解析。include include in...