Summary of the code for intercepting Chinese strings in php
Source: Internet
Author: User
As we all know, the native function substr of php does not support intercepting Chinese strings. The following code provides a variety of methods to intercept php Chinese strings. String truncation is a very common programming task, which is often used for string truncation with Chinese characters. Although it is not difficult, it takes time to write a function. here we will introduce a good string truncation function to meet basic requirements.
1. truncate the GB2312 Chinese string Copy codeThe code is as follows:
<? Php
// Truncate a 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;
}
?>
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