-
- /*
- * Function: The function is the same as substr, except that it will not cause garbled characters
- * Parameters:
- * Return:
- */
- function utf8_substr( $str , $start , $length =null ){
- // First intercept normally.
- $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 ){
- // intercept 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 complete The first byte of the character, and 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{
- // intercept 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, intercept it later 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 comply with 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 match 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;
- }
- }
- }
- return $res;
- }
- ?>
Copy code
Test ---
-
- $str = 'dfjdjf test 13f test 65&2 datafddj(1 on mfe&...on';
- var_dump( utf8_substr( $str , 22 , 12 ) ); echo '
';
- var_dump( utf8_substr( $str , 22 , -6 ) ); echo '
';
- var_dump( utf8_substr( $str , 9 , 12 ) ); echo '
';
- var_dump( utf8_substr( $str , 19 , 12 ) ); echo '
';
- var_dump( utf8_substr( $str , 28 , -6 ) ); echo '
?>
Copy code
Display results: (interception without garbled characters)
string(12) "According to fdj"
string(26) "According to fdj (1 is mfe&..."
string(13) "13f try 65&2 number"
string(12) "data fd"
string(20) "dj(1justmfe&..."
|