-
- /**
- * Complete word interception
- * Edited and organized by bbs.it-home.org
- * @param $str
- * @param $start
- * @param $length
- *
- * @return string
- */
- public static function usubstr($str, $start, $length = null)
- {
-
- // Intercept normally first.
- $res = substr($str, $start, $length);
- $strlen = strlen($str);
-
- /* Then determine whether the first and last 6 bytes are complete (not incomplete) */
- // If the parameter start is a positive number
- if ($start >= 0) {
- //Truncate about 6 bytes 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 first byte is not the first byte of the complete character, then intercept about 6 bytes
- $prev_start = $start - 6 > 0 ? $start - 6 : 0;
- $prev_segm = substr($str, $prev_start, $start - $prev_start);
- } // start is a negative number
- else {
- // Cut about 6 bytes forward
- $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 first byte is not the first byte of the complete character, then intercept about 6 bytes later.
- $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 match utf8 rules
- if (preg_match('@^([x80-xBF]{0,5})[xC0-xFD]?@', $next_segm, $bytes)) {
- if (!empty($bytes[1] )) {
- $bytes = $bytes[1];
- $res .= $bytes;
- }
- }
- // Determine whether the last 6 bytes comply with utf8 rules
- $ord0 = ord($res[0]);
- if (128 <= $ord0 && 191 >= $ord0) {
- // Intercept from the back and add it in front of res.
- if (preg_match('@[xC0-xFD][x80-xBF]{ 0,5}$@', $prev_segm, $bytes)) {
- if (!empty($bytes[0])) {
- $bytes = $bytes[0];
- $res = $bytes . $res;
- }
- }
- }
- if (strlen($res) < $strlen) {
- $res = $res . '...';
- }
- return $res;
- }
Copy code
|