Example of the substr_for_gb2312 and substr_for_utf8 functions customized by php,

Source: Internet
Author: User

Example of the substr_for_gb2312 and substr_for_utf8 functions customized by php,

This article describes the usage of the substr_for_gb2312 and substr_for_utf8 functions customized by php. We will share this with you for your reference. The details are as follows:

/** Gb2312 Chinese string truncation */function substr_for_gb2312 ($ str, $ start, $ len = null) {$ totlelength = strlen ($ str ); // special case if ($ len = null) $ len = $ totlelength; if ($ len = 0) return ""; if ($ len >=$ totlelength & $ start = 0) return $ str; if ($ start> $ totlelength) return ""; // analyze the location when $ start if ($ start <0) // $ start <0 is converted to $ start> 0. {if (abs ($ start) >=$ totlelength) $ start = 0; else $ start = $ totlelength-AB S ($ start) ;}// determine the start position. When the start position Splits a Chinese character, the returned value contains this Chinese character. if ($ start> 0) {$ I = $ start-1; $ flag =-1; while ($ I> = 0) {if (ord (substr ($ str, $ I, 1)> 160) {$ flag =-1 * $ flag;} else break; $ I --;} if ($ flag = 1) {$ start = $ start-1; $ len ++; // ensure no displacement .}} $ str = substr ($ str, $ start); // remove the $ start character before the $ start character of the string $ str $ totlelength = strlen ($ str); // determine the end position, when the delimiter Splits a Chinese character, the returned value does not contain this Chinese character. if ($ len <0) $ len = $ totlelength-abs ($ len); if ($ Len <= 0) return ""; $ I = min ($ len, $ totlelength); $ I --; $ flag =-1; while ($ I> = 0) {if (ord (substr ($ str, $ I, 1)> 160) {$ flag =-1 * $ flag;} else break; $ I --;} if ($ flag = 1) $ len = $ len-1; $ subit = substr ($ str, 0, $ len); return $ subit ;} /*************************************** * ************************** PHP intercepts the UTF-8 string, solve the half-character problem. * English letters, numbers (halfwidth) are 1 byte (8 bits), and Chinese characters (fullwidth) are 3 bytes * @ return retrieved strings. When $ len is less than or equal to 0, returns the length of the substring on the left of the entire string * @ param $ str source string * $ len ******************** **************************************** * ***/function substr_for_utf ($ str, $ len) {for ($ I = 0; $ I <$ len; $ I ++) {$ temp_str = substr ($ str, 0, 1 ); if (ord ($ temp_str)> 127) {$ I ++; if ($ I <$ len) {$ new_str [] = substr ($ str ); $ str = substr ($ str, 3) ;}} else {$ new_str [] = substr ($ str,); $ str = substr ($ str, 1) ;}} re Turn join ($ new_str) ;}?> /* Utf8 truncation function with the start position */function utf8_substr ($ string, $ start, $ length) {preg_match_all ('/[/x00-/x7F] | [/xC2-/xDF] [/x80-/xBF] | [/xE0-/xEF] [/x80 -/ xBF] [/x80-/xBF] | [/xF0-/xF4] [/x80-/xBF] [/x80-/xBF] [/x80-/xBF]/', $ string, $ rs); $ out = ''; $ size = count ($ rs [0]); $ end = $ start + $ length; if ($ end> $ size) {$ end = $ size;} for ($ I = $ start; $ I <$ end; $ I ++) {$ out. = $ rs [0] [$ I];} return $ out ;}

Supplement: The editor recommends a php formatting and formatting typographical tool on this site to help you typeset code in future PHP programming:

Php code online formatting and beautification tools:

Http://tools.jb51.net/code/phpformat

In addition, because php belongs to the C language style, the following tool can also format php code:

C language style/HTML/CSS/json code formatting and beautification tools:
Http://tools.jb51.net/code/ccode_html_css_json

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.