Let me first introduce you to the function used:
Copy Code code as follows:
Mb_strwidth ($STR, $encoding) returns the width of the string
$str the string to be evaluated
$encoding the encoding to use, such as UTF8, GBK
Copy Code code as follows:
Mb_strimwidth ($str, $start, $width, $tail, $encoding) intercept strings by width
$str the string to intercept
$start where to start intercepting, the default is 0
$width the width to intercept
$tail append to the string behind the intercept string, the common ...
$encoding the encoding to use
below to give you examples demo:
Copy Code code as follows:
<?php
/**
* UTF8 encoding format
* 1 Chinese occupies 3 bytes
* We want 1 Chinese to occupy 2 bytes,
* Because the position of 2 letters from the width is equivalent to 1 Chinese
*/
Test string
$STR = ' aaaa ah aaaa ah ah ah aaa ';
echo strlen ($STR); Output to 25 bytes only with strlen
You must specify the encoding, or you will use PHP's inner Code mb_internal_encoding () to view the inner code
Use the Mb_strwidth output string with a width of 20 using UTF8 encoding
Echo mb_strwidth ($str, ' UTF8 ');
Intercept only if the width is greater than 10
if (Mb_strwidth ($str, ' UTF8 ') >10) {
Here set the Intercept starting from 0, take 10 append ..., use UTF8 encoding
Pay attention to the additional ... will also be counted into the length
$str = mb_strimwidth ($str, 0, ' ... ', ' UTF8 ');
}
The final output aaaa ah ... 4 A, 4, 1, 2 3, 3 4+2+3=9.
is not very simple ah, some people say why is 9 not 10?
Because the right "ah" behind or "ah", Chinese 2, 9+2=11 exceeded the set, so remove one is 9
Echo $str;
Let me introduce you to some other functions below:
Copy Code code as follows:
Mb_strlen ($STR, $encoding) returns the length of the string
$str the string to be evaluated
Encoding used by the $encoding
Copy Code code as follows:
Mb_substr ($str, $start, $length, $encoding) intercept string
$str the string to intercept
Where do $start begin to intercept?
How long does $length intercept?
Encoding used by the $encoding
In fact, these 2 functions and strlen (), substr () is very similar, the only difference is that you can set the encoding.
Example below:
Copy Code code as follows:
<?php
/**
* UTF8 encoding format
* 1 Chinese occupies 3 bytes
*/
$str = ' aa12 AA ';
echo strlen ($STR); Direct output Length of 9
The output length is 7, why 7?
Note that after setting the encoding, whether in Chinese or English, each length is 1
A a 1 2 ah a A
1+1+1+1+1+1+1 = 7
Is that exactly 7 characters?
Echo Mb_strlen ($str, ' UTF8 ');
Same mb_substr, too.
I just want 5 characters now.
Echo mb_substr ($str, 0, 5, ' UTF8 '); Output Aa12 AH
In fact, there are many useful functions in MB extension, here is not to enumerate.
Interested friends can view the Official Handbook
Well, let's introduce it to you today.