PHP English string truncation code sharing _ php instance that can ensure word integrity

Source: Internet
Author: User
This article mainly introduces PHP English strings that can ensure word integrity. The Code contains detailed comments. For more information, see the following code:

/*** Intercept complete words ** @ param $ str * @ param $ start * @ param $ length ** @ return string */public static function usubstr ($ str, $ start, $ length = null) {// You can intercept it again. $ res = substr ($ str, $ start, $ length); $ strlen = strlen ($ str);/* Then judge whether 6 bytes at the beginning and end are complete (not incomplete) * /// if the start parameter is positive, if ($ start> = 0) {// truncates about 6 bytes to the forward, $ next_start = $ start + $ length; // initial position $ next_len = $ next_start + 6 <= $ strlen? 6: $ strlen-$ next_start; $ next_segm = substr ($ str, $ next_start, $ next_len); // If the 1st byte is not the first byte of the complete character, next, take about 6 bytes. $ prev_start = $ start-6> 0? $ Start-6: 0; $ prev_segm = substr ($ str, $ prev_start, $ start-$ prev_start );} // start is a negative else {// forward and then intercept about 6 bytes $ next_start = $ strlen + $ start + $ length; // initial position $ next_len = $ next_start + 6 <= $ strlen? 6: $ strlen-$ next_start; $ next_segm = substr ($ str, $ next_start, $ next_len); // If the 1st byte is not the first byte of the complete character, next, we can take about 6 bytes. $ start = $ strlen + $ start; $ prev_start = $ start-6> 0? $ Start-6: 0; $ prev_segm = substr ($ str, $ prev_start, $ start-$ prev_start );} // determine whether the first 6 bytes meet the utf8 Rule if (preg_match ('@ ^ ([x80-xBF] {}) [xC0-xFD]? @ ', $ Next_segm, $ bytes) {if (! Empty ($ bytes [1]) {$ bytes = $ bytes [1]; $ res. = $ bytes ;}/// determine whether the last 6 bytes meet the utf8 rule $ ord0 = ord ($ res [0]); if (128 <= $ ord0 & 191> = $ ord0) {// truncate the image and add it to the front of res. if (preg_match ('@ [xC0-xFD] [x80-xBF] {} $ @', $ prev_segm, $ bytes) {if (! Empty ($ bytes [0]) {$ bytes = $ bytes [0]; $ res = $ bytes. $ res ;}}if (strlen ($ res) <$ strlen) {$ res = $ res. '... ';} return $ res ;}

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.