Php string truncation Function

Source: Internet
Author: User

 

Php tutorial string truncation Function
In php, there is a string truncation function substr, but this function can only intercept English and does not support Chinese

Its ing. Below we provide three functions that support Chinese and English ing.

 

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 = "";
$ Length = "18 ";
$ Append = false;
Echo syssubstr ($ string, $ length, $ append );

String truncation function 2

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 );
$ Tmps tutorial tr = '';

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 = "New php training ";
Echo cut_str ($ str, 8, 0, 'gb2312 ');


Function 3

// Truncate 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 );
}
?>


All the above three functions are very good at intercepting Chinese characters.

.


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.