Chinese character string Truncation in various PHP Codes

Source: Internet
Author: User

Although PHP has a ready-made string truncation function substr (), this function cannot intercept Chinese character strings. To achieve this effect, we need to write the corresponding functions ourselves. There are a variety of Chinese character encoding, such as GB2312, UTF-8, Chinese character string interception needs to distinguish this Chinese character encoding, the following is a few solutions.

Truncates GB2312 Chinese strings.
<? Php // intercept the Chinese string-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 ;}?>
Truncates UTF-8 encoded multi-byte strings.
<? Php // intercept the utf8 string 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) ;}?>
Chinese character truncation functions supported by UTF-8 and GB2312
<? Php/* Utf-8, gb2312 are supported by the Chinese character truncation function cut_str (string, truncation length, start length, encoding ); the default encoding start length is UTF-8. The default value is 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 (co Unt ($ 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 =" string to be intercepted by abcd "; echo cut_str ($ str, 8, 0, 'gb2312');?>
BugFree character truncation Function
<?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.bkjia.com";$Length = "18";$Append = false;echo sysSubStr($String,$Length,$Append);?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.