Php program for intercepting Chinese strings-PHP source code

Source: Internet
Author: User
We can use built-in functions to intercept strings in php, but built-in functions do not support intercept Chinese characters. If you need to intercept Chinese strings, we need to perform some operations. I will introduce them to you. We can use built-in functions to intercept strings in php, but built-in functions do not support intercept Chinese characters. If you need to intercept Chinese strings, we need to perform some operations. I will introduce them to you.

Script ec (2); script

Code for GB2312

The Code is as follows:

// $ Str is the string to be intercepted
// $ Start is the starting position of the characters to be captured
// $ Len refers to the length of characters to be intercepted
Function sub_str ($ 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 ."...";
}

For uft8

The Code is as follows:

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

The above method is definitely not practical, because I hope to automatically identify any encoded string interceptions, and then I will share it with my friends.

The Code is as follows:

<? Php
/**
* @ Package BugFree
* @ Version $ Id: FunctionsMain. inc. php, v 1.32 11:38:37 wwccss Exp $
*
*
* Return part of a string (Enhance the function substr ())
*
* @ Author Chunsheng Wang
* @ 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.111cn.net is at the forefront of China's automated testing ";
$ 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.