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 ;}