Php coding problems

Source: Internet
Author: User
Php coding problems






Echo strlen ("Star ");
?>




Why does echo strlen ("Star") output 3?
The strlen () function returns the length of a string, measured in characters.
1 Chinese character = 2 bytes Output, isn't it 2?
Is it related to encoding?


Reply to discussion (solution)

Echo mb_strlen ("Star", 'gbk ');


1

Echo strlen ("Star") outputs 3
It indicates that your program file is saved as UTF-8 encoded.

During strlen calculation, the Chinese character for UTF8 is 3 characters in length.
This is a fixed value, so ignore why.
Because I don't know either.

In UTF-8 encoding, a Chinese character occupies 3 bytes, so 3 is returned.

This function has a very large relationship with Encoding. you must select the correct encoding to obtain the correct value.

I learned how to unify coding.

In UTF-8, Chinese characters are 3 characters

  

3 UTF-8

What is UTF8 in Chinese? Character


Echo strlen ("Star ");

Both UTF-8 and gbk output 3
Both are output 2 after reinstallation.
I am totally messy.

Echo mb_strlen ("Star", 'gbk ');

1




Why?
Echo mb_strlen ("Star", 'gbk'); output is 1
Echo mb_strlen ("Star", 'utf-8'); I output 1

    


Output is 2 EUC-CN

Echo strlen ("Star ");
How to output 3 in UTF-8
Output 2 under gbk

Save your file as UTF-8 encoded in UTF-8. Save as ANSI in gbk format.


Echo strlen ("Star ");

Both UTF-8 and gbk output 3
Both are output 2 after reinstallation.
I am totally messy.



It is not the encoding, but the encoding when your php file is saved.
UTF8 Chinese is 3 characters
GBK Chinese is 2 characters






What is your software name? I also want to download it.
I used to write all the code in Visual Studio 2013, and changed the suffix to php, which often encountered some strange problems.

Last small question
Echo mb_strlen ("Star", 'gbk ');
UTF-8 output 2
Output 1 in ansi

Echo mb_strlen ("Star", 'utf-8 ');
UTF-8 output 1
Output 1 in ansi

What is this ??

It is not the encoding, but the encoding when your php file is saved.
UTF8 Chinese is 3 characters
GBK Chinese is 2 characters



Cannot be transcoded?

No!

Only notify the browser to interpret the content with the character set specified by charset
If your program file is UTF-8, declaring charset = gbk will only cause garbled characters.
Without On the contrary, the browser's automatic identification function will not cause garbled characters.

Setting file encoding is different from declaring charset. if you need to set file encoding, the general software can solve this problem, such as notepad ++.

Echo mb_strlen ("Star", 'gbk ');
UTF-8 output 2
Output 1 in ansi

Echo mb_strlen ("Star", 'utf-8 ');
UTF-8 output 1
Output 1 in ansi

What is this ??






What is your software name? I also want to download it.
I used to write all the code in Visual Studio 2013, and changed the suffix to php, which often encountered some strange problems.



Editplus

? Why does mb_strlen have a look ?? Http://developer.51cto.com/art/201105/263103.htm

This is understandable.
Echo mb_strlen ("Star", 'gbk ');
UTF-8 outputs 2 // A Chinese character occupies three bytes under UTF-8, and gbk occupies two bytes. in the case of gbk, it is 3/2 = 1.5 characters, A half character is also counted as one, so 2 is output.
Output 1 in ansi

Echo mb_strlen ("Star", 'utf-8 ');
UTF-8 output 1
Output 1 in ansi // 2/3 is less than 1, which is also calculated by 1.

Therefore, when mb_strlen is used to check the number of characters contained in a string, unified encoding will not be problematic.

Thank you. I understand.

Echo mb_strlen ("Star", 'gbk ');
UTF-8 output 2
Output 1 in ansi

Echo mb_strlen ("Star", 'utf-8 ');
UTF-8 output 1
Output 1 in ansi

What is this ??


Different results are displayed in different environments, because you use parameters incorrectly!
The encoding parameter of the mb series functions is used to specify the character set of the string to be operated. If an error is returned, the natural result is returned.
Therefore, the mb_detect_encoding or mb_check_encoding function is used to obtain the actual character set of the string.
$ S = 'star'; $ charset = mb_detect_encoding ($ s, 'utf-8, gbk'); echo mb_strlen ($ s, $ charset );
$charset = mb_check_encoding($s, 'utf-8') ? 'utf-8' : 'gbk';echo mb_strlen($s, $charset);

Because mb_detect_encoding is prone to misjudgment, a more accurate mb_check_encoding function is added.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.