How to intercept PHP English strings to ensure word integrity
Today, I will talk to you about how to ensure the integrity of the PHP English string interception, many people may not understand, in order to make you better understand, the editor summed up the following, I hope you can get something according to this article.
Go directly to the code:
/ * * intercept complete words * * @ param $str * @ param $start * @ param $length * * @ return string * / public static function usubstr ($str, $start, $length = null) {/ / first intercept normally. $res = substr ($str, $start, $length); $strlen = strlen ($str); / * then determine whether each 6 bytes is complete (not incomplete) * / / if the parameter start is a positive number if ($start > = 0) {/ / intercept about 6 bytes forward $next_start = $start + $length; / / initial position $next_len = $next_start + 60? $start-6: 0 $prev_segm = substr ($str, $prev_start, $start-$prev_start);} / / start is negative else {/ / go forward to intercept about 6 bytes $next_start = $strlen + $start + $length; / / initial position $next_len = $next_start + 60? $start-6: 0; $prev_segm = substr ($str, $prev_start, $start-$prev_start) } / / determine whether the first 6 bytes conform to the utf8 rule if (preg_match ('@ ^ ([x80-xBF] {0jue 5}) [xC0-xFD]? @', $next_segm, $bytes) {if (! empty ($bytes [1])) {$bytes = $bytes [1]; $res. = $bytes }} / / determine whether the last 6 bytes comply with the utf8 rule $ord0 = ord ($res [0]); if (128 = $ord0) {/ / intercept later and add it to the front of res. If (preg_match ('@ [xC0-xFD] [x80-xBF] {0jue 5} $@', $prev_segm, $bytes)) {if (! empty ($bytes [0])) {$bytes = $bytes [0]; $res = $bytes. Res;}} if (strlen ($res) < $strlen) {$res = $res. '...';} return $res;} after reading the above, do you have any further understanding of how to ensure the integrity of the PHP English string interception? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.