Get the App
SLTechnology News&Howtos  ›  Development  › 

Php user-defined Chinese string interception function substr_for_gb2312 and how to use substr_for_utf8

Shulou Source: shulou.com Published: 2022-06-03 19:49:21 09月29日 Update

This article mainly introduces the php custom Chinese string interception function substr_for_gb2312 and substr_for_utf8 how to use, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.

The details are as follows:

/ * * gb2312 Chinese string interception * / function substr_for_gb2312 ($str,$start,$len=null) {$totlelength = strlen ($str); / / Special case if ($len= = null) $len= $totlelength; if ($len= = 0) return ""; if ($len > = $totlelength & & $start = 0) return $str; if ($start > $totlelength) return "; / / analyze $start if ($start)

< 0 ) //$start0时的定位. { if ( abs($start) >

= $totlelength) $start = 0; else $start = $totlelength-abs ($start);} / / determine the starting position. When the starting bit splits a Chinese character, the return value contains the Chinese character. If ($start > 0) {$I = $start-1; $flag=-1; while ($I > = 0) {if (ord (substr ($str,$i,1)) > 160) {$flag=-1 housekeeping;} else break; $iLue;} if ($flag==1) {$start = $start-1; $len++; / / guarantee no displacement. } $str = substr ($str,$start); / / truncate the character before the $start bit of the string $totlelength = strlen ($str); / / determine the end position, when the end bit splits a Chinese character, the return value does not contain the Chinese character. If ($len160) {$flag=-1*$flag;} else break; $iMurray;} if ($flag= = 1) $len=$len-1; $subit=substr ($str,0,$len); return $subit } / * PHP intercepts the UTF-8 string to solve the half-character problem. * English, numeric (half-width) is 1 byte (8 bits) Chinese (full width) is a 3-byte string taken out by * @ return, when $len is less than or equal to 0 The length of the entire string * @ param $str source string * $len left substring * / function substr_for_utf ($str,$len) {for ($iTun0) is returned * * utf8 interception function with start location * / function utf8_substr ($string, $start, $length) {preg_match_all ('/ [/ x00-/x7F] | [/ xC2-/xDF] [/ x80-/xBF] | [/ xE0-/xEF] [/ x80-/xBF] [/ x80-/xBF] | [/ xF0-/xF4] [/ x80-/xBF] [/ x80-/xBF] [/ x80-/xBF] /, $string, $rs); $out ='' $size = count ($rs [0]); $end = $start + $length; if ($end > $size) {$end = $size;} for ($I = $start; $I < $end; $iTunes +) {$out. = $rs [0] [$I];} return $out } Thank you for reading this article carefully. I hope the article "php Custom Chinese string interception function substr_for_gb2312 and how to use substr_for_utf8" shared by the editor will be helpful to you. At the same time, I hope you will support us and follow the industry information channel. More related knowledge is waiting for you to learn!

Tags: Character string Chinese article Chinese character function position byte start value interest half corner at the same time situation number more friends special cases knowledge editing Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info MariaDB Xiaomi Shulou Information Huawei