Referring to the Chinese and English mixed row count, interception, we first think of is the ASCII, 16 binary, regular matching, cycle count.
What I am sharing with you today is the MB extension of PHP, which teaches you how to handle strings easily.
First, we introduce the functions used:
Mb_strwidth ($STR, $encoding) returns the width of the string
$str the string to evaluate
$encoding the encoding to use, such as UTF8, GBK
Mb_strimwidth ($str, $start, $width, $tail, $encoding) to intercept a string by width
$str the string to intercept
$start where to start interception, the default is 0
$width the width to intercept
$tail append to the string behind the intercept string, commonly used is ...
$encoding the encoding to use
The following example shows you:
!--?
php
/*
* * UTF8 encoding format * 1 Chinese takes 3 bytes * We want 1 Chinese to occupy 2 bytes, * Because 2 letters are occupied from the width Position equivalent to 1 Chinese
*/
//
test string
$str = ' aaaa ah ah aaaa ah ah aaa '
;
Echo
strlen (
$str );
//
use only strlen output to 25 bytes//must specify the encoding, otherwise it will use PHP's internal code mb_internal_encoding () can view the code//use Mb_strwidth output string 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
) {
//
set here to intercept from 0, fetch 10 append ..., use UTF8 encode/ /Note the additional ... will also be counted within the length of
$str = mb_strimwidth (
$str , 0, ' ... ', ' UTF8 '
);}
//
final output aaaa ah ... 4 A Count 4 1 Ah count 2 3 points count 3 4+2+3=9//is not very simple ah, some people said why 9 is not 10? Because just "ah" behind or "ah", Chinese count 2, 9+2=11 beyond the set, so remove one is 9
echo
$str ;
Let us introduce some other functions below:
Mb_strlen ($STR, $encoding) returns the length of a string
$str the string to evaluate
Encoding used by $encoding
Mb_substr ($str, $start, $length, $encoding) intercept string
$str the string to intercept
Where do $start start the interception?
How long does the $length intercept?
Encoding used by $encoding
In fact, these 2 functions and strlen (), substr () very much like, the only difference is that you can set the encoding.
Bottom up Example:
PHP/**/$str = ' aa12 ah aa '; Echo strlen ($str// Direct output length is 9//output length is 7, why is 7? Note that after setting the code, whether it is Chinese or English each length is 1//a a 1 2 ah A//1+1+1+1+1+1+1 = 7//is exactly 7 characters Ah echo mb_strlen ($str /c13>, ' UTF8 '); // same MB_SUBSTR is the same//I only want 5 characters now echo mb_substr ($str// output aa12 Ah
In fact, the MB extension inside a lot of useful functions, here will not give you a list.
Interested friends can view the Official Handbook
http://www.php.net/manual/zh/ref.mbstring.php
Well, I'll introduce you here today.
http://www.bkjia.com/PHPjc/777158.html www.bkjia.com true http://www.bkjia.com/PHPjc/777158.html techarticle referring to the Chinese and English mixed row count, interception, we first think of is the ASCII, 16 binary, regular matching, cycle count. Today I share with you is the PHP MB extension, teach you how to easily handle ...