***PHP各種編碼的漢字字串擷取
雖然PHP有現成的擷取字串函式substr(),但是這個函式不能對漢字字串進行擷取,要實現這種效果還需要我們自己去編寫相應的函式。漢字有多種編碼,比如GB2312,UTF-8等,漢字字串的擷取需要區分這種漢字編碼,下面是給出的幾個解決方案。
擷取GB2312中文字串
<?php //擷取中文字串- function mysubstr($str, $start, $len) { $tmpstr = ""; $strlen = $start + $len; for($i = 0; $i < $strlen; $i++) { if(ord(substr($str, $i, 1)) > 0xa0) { $tmpstr .= substr($str, $i, 2); $i++; } else $tmpstr .= substr($str, $i, 1); } return $tmpstr; } ?>
擷取utf8編碼的多位元組字串
<?php //擷取utf8字串 function utf8Substr($str, $from, $len) { return preg_replace(`#^(?:[x00-x7F]|[xC0-xFF][x80-xBF]+){0,`.$from.`}`. `((?:[x00-x7F]|[xC0-xFF][x80-xBF]+){0,`.$len.`}).*#s`, `$1`,$str); } ?>
UTF-8、GB2312都支援的漢字擷取函式
<?php /* Utf-8、gb2312都支援的漢字擷取函式 cut_str(字串, 擷取長度, 開始長度, 編碼); 編碼預設為 utf-8 開始長度預設為 0 */function cut_str($string, $sublen, $start = 0, $code = `UTF-8`) { if($code == `UTF-8`) { $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; } } $str = "abcd需要擷取的字串"; echo cut_str($str, 8, 0, `gb2312`); ?>
BugFree 的字元擷取函式
<?php /** * @package BugFree * @version $Id: FunctionsMain.inc.php,v 1.32 2005/09/24 11:38:37 wwccss Exp $ * * * Return part of a string(Enhance the function substr()) * * @author Chunsheng Wang <[email]wwccss@263.net[/email]> * @param string $String the string to cut. * @param int $Length the length of returned string. * @param booble $Append whether append "...": false|true * @return string the cutted string. */ function sysSubStr($String,$Length,$Append = false) { if (strlen($String) <= $Length ) { return $String; } else { $I = 0; while ($I < $Length) { $StringTMP = substr($String,$I,1); if ( ord($StringTMP) >=224 ) { $StringTMP = substr($String,$I,3); $I = $I + 3; } elseif( ord($StringTMP) >=192 ) { $StringTMP = substr($String,$I,2); $I = $I + 2; } else { $I = $I + 1; } $StringLast[] = $StringTMP; } $StringLast = implode("",$StringLast); if($Append) { $StringLast .= "..."; } return $StringLast; } } $String = "www.nowamagic.net"; $Length = "18"; $Append = false; echo sysSubStr($String,$Length,$Append); ?>
如何聯絡我:【萬里虎】www.bravetiger.cn
【QQ】3396726884 (諮詢問題100元起,幫助解決問題500元起)
【部落格】http://www.cnblogs.com/kenshinobiy/
相關文章
- php 擷取中英文混合字串PHP字串
- JavaScript 擷取指定長度字串 區分漢字和英文字元JavaScript字串字元
- 字串擷取字串
- java中文字串漢字轉GBK編碼Java字串
- MySQL 字串函式:字串擷取MySql字串函式
- PHP擷取html文章PHPHTML
- jQuery字串擷取詳解jQuery字串
- 漢字編碼問題
- Linux下的字串擷取詳解Linux字串
- Shell中的字串擷取介紹字串
- shell指令碼擷取字串字尾名,檔名指令碼字串
- PHP 實現字串翻轉(包含中文漢字)的實現PHP字串
- php ffmpeg 視訊擷取PHP
- 字串擷取 slice,substr,substring 的區別字串
- 擷取第一個字用php生成一個頭像PHP
- JavaScript 擷取指定指定區間字串JavaScript字串
- shell 使用陣列及字串擷取陣列字串
- C#常用字串擷取C#字串
- Swift 4.0 字串擷取,拼接,字串富文字顯示Swift字串
- Python 漢字區位碼、字串 相互轉換Python字串
- PHP字串學習之如何返回漢字或混合字元的長度PHP字串字元
- 擷取 UTF8 編碼字串從首位元組開始指定寬度 (非長度)字串
- 獲取字串中的所有漢字字串
- PHP 查詢、擷取字串函式詳解PHP字串函式
- Java String類,字串常量池,建立方法,字串的獲取,擷取,轉換,分割。Java字串
- php 和 j s 對數字,字母,漢子等特殊字串 取反PHP字串
- sql常用函式詳解(一)——字串擷取SQL函式字串
- MySQL 字串擷取相關函式總結MySql字串函式
- Javascript之字串擷取函式slice()、substring()、substr()JavaScript字串函式
- 字元編碼及空白漢字佔位符字元
- 125 列舉實現PHP擷取中文不亂碼的實現方法PHP
- PHP中獲取當前頁面的各種URL格式PHP
- js 擷取檔案字尾名JS
- 擷取字串字串
- mysql 擷取指定的兩個字串之間的內容MySql字串
- 擷取字串的三種方法 substr,slice,substring的區別字串
- mysql 中如何取得漢字欄位的各漢字首字母MySql
- Golang 字串分割,替換和擷取 strings.SplitGolang字串
- PHP 將數字轉換為漢字PHP