Mentioned in the Chinese and English mixed row count, interception, we first thought of is ASCII, 16, regular match, loop count.
What I'm sharing today is PHP's MB extension, which teaches you how to handle strings easily.
Let me first introduce you to the function used:
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
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:
<?php
/**
* UTF8 encoded format
* 1 Chinese 3 bytes
* What we want is 1 Chinese to occupy 2 bytes,
* Because the position of 2 English letters from the width is equivalent to 1 Chinese
* *
//Test string
$str = ' aaaa ah aaaa ah ah ah aaa ';
echo strlen ($STR); Only strlen output to 25 bytes
//must be specified encoding, or you will use PHP's internal code mb_internal_encoding () can view the internal code
//Use Mb_ The width of the strwidth output string is 20 using UTF8 encoded
echo mb_strwidth ($str, ' utf8 ');
Only the width is greater than 10 to intercept
if (Mb_strwidth ($str, ' UTF8 ') >10) {
//here set to intercept from 0, take 10 append ..., use UTF8 encoding
//Note appended ... will also be computed to the length
$str = mb_strimwidth ($str, 0, ' ... ', ' UTF8 ');
}
The final output aaaa ah ... 4 A, 4, 1, 2 3, 3 4+2+3=9./
/View This column more highlights: http://www.bianceng.cnhttp://www.bianceng.cn/webkf/PHP/
// 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:
Mb_strlen ($STR, $encoding) returns the length of the string
$str the string to be evaluated
Encoding used by the $encoding
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:
<?php
/**
* UTF8 encoded format
* 1 3 bytes in Chinese * *
*
$str = ' aa12 aa ';
echo strlen ($STR); The direct output length is 9
//The output length is 7, why is 7?
//Note that after setting the encoding, whether in Chinese or English each length is 1
/a a 1 2 ah a
//1+1+1+1+1+1+1 = 7
//is not exactly 7 characters Ah
echo Mb_strlen ( $str, ' UTF8 ');
The same mb_substr is the same
//I now only want 5 characters
Echo mb_substr ($str, 0, 5, ' UTF8 ');/output Aa12
In fact, there are many useful functions in MB extension, here is not to enumerate.
Interested friends can view the Official Handbook
http://www.php.net/manual/zh/ref.mbstring.php
Well, let's introduce it to you today.