Home >Backend Development >PHP Tutorial >PHP Chinese and English mixed string interception method
Chinese and English mixed counting and interception, no custom functions are needed, but the mb extension of PHP is used, and the original PHP function is used to easily handle string interception. First, let’s introduce common functions to intercept strings. mb_strwidth($str, $encoding) returns the width of the string $str The string to be calculated $encoding The encoding to use, such as utf8, gbk mb_strimwidth($str, $start, $width, $tail, $encoding) intercepts string according to width $str The string to be intercepted $start From which position to intercept, the default is 0 $width The width to be intercepted $tail is appended to the string after the intercepted string. Commonly used ones are... $encoding The encoding to use Example:
Other string interception functions: mb_strlen($str, $encoding) returns the length of the string $str The string to be calculated $encoding The encoding to use mb_substr($str, $start, $length, $encoding) intercepts string $str The string to be intercepted $start Where to start intercepting $length intercepts the length $encoding encoding to use In fact, these two functions are very similar to strlen() and substr(). The only difference is that the encoding can be set. The above two examples of string interception functions.
There are many practical functions in the mb extension library, which are not introduced one by one here. If you are interested, you can refer to the relevant content in the PHP manual. |