Home >Backend Development >PHP Tutorial >PHP can easily intercept mixed Chinese and English strings with just 2 lines of code!
When it comes to Chinese and English mixed counting and interception, the first thing that comes to mind is ascii, hexadecimal, regular matching, and loop counting. Today I will share with you the mb extension of php to teach you how to process strings easily.
First introduce to you the functions used: mb_strwidth($str, $encoding) returns the width of the string $str The string to be calculated $encoding The encoding to be used, such as utf8, gbk mb_strimwidth($str, $start, $width, $tail, $encoding) intercepts the string by width $str The string to be intercepted $start The position from which to start interception, the default is 0 $width The width to be intercepted $tail is appended to the string after the intercepted string, commonly used is... $encoding The encoding to use
I will give you an example below:
Let me introduce some other functions to you below: mb_strlen($str, $encoding) returns the length of the string $str The string to be calculated $encoding The encoding used mb_substr($str, $start, $length, $encoding) intercepts the string $str The string to be intercepted $start Where to start intercepting $length How long to intercept $encoding The encoding used In fact, these two functions are very similar to strlen() and substr(). The only difference is that the encoding can be set.
Example below:
In fact, there are many useful functions in the mb extension, so I won’t list them all here. Interested friends can check the official manual http://www.php.net/manual/zh/ref.mbstring.php Okay, that’s all for today.
|