PHP Coding Issues

Source: Internet
Author: User







Echo strlen ("star");
?>




Why Echo strlen ("star") output is 3
The strlen () function returns the length of a string, measured in characters.
1 Kanji = 2 bytes Output is not 2?
Is it related to coding?


Reply to discussion (solution)

Echo Mb_strlen ("star", ' GBK ');


1

Echo strlen ("star") output is 3
Indicates that your program files are saved as Utf-8 encoded.

When strlen is calculated, the Chinese character that treats a UTF8 is 3 lengths
This is a fixed value, regardless of why.
Because I don't know.

Under Utf-8 encoding, a Chinese character occupies 3 bytes, so it returns 3.

This function and coding have a very large relationship, need to choose the correct code to get the correct value

Learn the code to unify AH

Utf-8, Chinese is 3 characters

  

3 UTF-8

UTF8 is Chinese is a three-character


Echo strlen ("star");

My utf-8 or GBK are output 3
Both are output 2 after reloading
I'm completely messy.

Echo Mb_strlen ("star", ' GBK ');

1




Why
Echo Mb_strlen ("star", ' GBK '); Output is 1
Echo Mb_strlen ("star", ' Utf-8 '); I'm outputting 1.

    


Output is 2 EUC-CN

Echo strlen ("star");
How to Output 3 under Utf-8
Output 2 under GBK

Saving your file as Utf-8 encoding is under Utf-8. The Save as ANSI format is GBK.


Echo strlen ("star");

My utf-8 or GBK are output 3
Both are output 2 after reloading
I'm completely messy.



This is not the code, but the encoding of your PHP file when it is saved.
UTF8 Chinese is 3 characters
GBK Chinese is 2 characters






What is your name for this software, I also want to download
I used to write the code in Visual Studio 2013, and then change the suffix to PHP, there are often some strange problems

One last little question.
Echo Mb_strlen ("star", ' GBK ');
Utf-8 Output 2
ANSI down Output 1

Echo Mb_strlen ("star", ' Utf-8 ');
Utf-8 Output 1
ANSI down Output 1

What's the matter??

This is not the code, but the encoding of your PHP file when it is saved.
UTF8 Chinese is 3 characters
GBK Chinese is 2 characters



Can't you transcode it?

No!

Just notifies the browser to interpret the content with a character set specified by CharSet
If your program file is Utf-8, then declaring CHARSET=GBK will only cause garbled
Instead of the browser automatically recognize the function, does not appear garbled

Settings file encoding and declaration charset is different, if you need to set the file encoding, the general software can be resolved, such as notepad++

Echo Mb_strlen ("star", ' GBK ');
Utf-8 Output 2
ANSI down Output 1

Echo Mb_strlen ("star", ' Utf-8 ');
Utf-8 Output 1
ANSI down Output 1

What's the matter??






What is your name for this software, I also want to download
I used to write the code in Visual Studio 2013, and then change the suffix to PHP, there are often some strange problems



EditPlus

Mb_strlen can look at it?? Http://developer.51cto.com/art/201105/263103.htm

can understand that.
Echo Mb_strlen ("star", ' GBK ');
Utf-8 Output 2//A Chinese is three bytes under the Utf-8, two bytes under GBK, 3/2 = 1.5 characters, and half a character, so output 2
ANSI down Output 1

Echo Mb_strlen ("star", ' Utf-8 ');
Utf-8 Output 1
ANSI Output 1//Here is 2/3 is less than 1, also by 1 calculation.

So when using Mb_strlen to detect the number of characters contained in a string, there is no problem with encoding unification.

Thank you, I understand.

Echo Mb_strlen ("star", ' GBK ');
Utf-8 Output 2
ANSI down Output 1

Echo Mb_strlen ("star", ' Utf-8 ');
Utf-8 Output 1
ANSI down Output 1

What's the matter??


There are different results in different environments, because you are using the wrong parameters!
The encoding parameter of the MB series function is used to indicate the character set of the string being manipulated, giving the wrong natural result.
So when using the mb_detect_encoding or mb_check_encoding function to get 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 miscalculation, 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.