PHP uses the mb_string function library to process windows-related Chinese characters and enable the PHPMb_String method in windows.

Source: Internet
Author: User
Yesterday, I tried to batch process a bunch of previously downloaded files and matched the key content in the files with regular expressions for centralized processing. An error occurred while operating the file, that is, encoding in the windows operating system.

Yesterday, I tried to batch process a bunch of previously downloaded files and matched the key content in the files with regular expressions for centralized processing. An error occurred while operating the file, that is, encoding in the windows operating system.

Yesterday, I tried to batch process a bunch of previously downloaded files and matched the key content in the files with regular expressions for centralized processing. An error occurred while operating the file, that is, encoding in the windows operating system.

We all know that in windows (of course, the Chinese version), file name and file content encoding are gbk, while in the development process, the encoding in IDE is UTF-8, (I will not discuss why, etc,

Only consider how to convert the encoding into the same) so the Chinese character in the regular pattern string that I wrote in The UTF-8 encoding does not match correctly in the gbk encoded file.

At first, I had no way. I tried to change the encoding of the PHP script file to GBK. It can also be used, but it was too low to think of this method, so find out if there are any functions in PHP that can meet my needs.

At this time, I thought of the iconv () function used to process file names in windows. Its function prototype is as follows:

String iconv (string $ in_charset, string $ out_charset, string $ str) Performs a character set conversion on the string str from in_charset to out_charset.

We often use:

$ Out_charset = 'utf-8'; $ fileName = iconv ($ fileName, $ out_charset, 'gbk ');

To process the file name, change the file name from gbk to UTF-8 without changing the content.

Additional manual translation:

If you add // Transcoder after the output string $ out_charset, that is, $ out_charset = 'utf-8 // transtranscoder ', when you encounter a character that cannot be converted to a UTF-8, the program will automatically replace it with a UTF-8 character of a similar character;

If you add // IGNORE that is $ out_charset = 'utf-8 // IGNORE 'After the output string $ out_charset, when you encounter a character that cannot be converted to a UTF-8, the program automatically skips this character.

If you do not add anything, the replacement will be interrupted when you encounter a character that cannot be replaced with a UTF-8.
However, when I use this function for processing, the result is as follows:

This indicates that the maximum number of characters processed by the iconv () function is only 64. The general file name size, while the content of my file is obviously more than 64 characters.

No way, I had to look for other functions again.

Until I found the mb_string function library, which is generally integrated in the PHP environment, we can find it in phpinfo.

In the mb_string function, there is a mb_convert_encoding () function that can change the encoding of a string. Its prototype is as follows:

String mb_convert_encoding (string $ str, string $ to_encoding [, mixed $ from_encoding]) Converts the character encoding of string str to to_encoding from optionally from_encoding.

The base type is similar to the iconv () function, but it does not modify the suffix of the output function, nor does it explicitly limit the length of the string.

In addition, we can see that $ from_encoding is optional and can automatically identify the source encoding.

Because you cannot find a specific character that cannot be transcoded, and you do not know how to handle a character that cannot be transcoded.

The whole file is processed through the mb_convert_encoding () function, so the problem is solved smoothly.

Finally, we will introduce the mb_string function library. Its full name is Multibyte String. Many of its methods are extended from PHP's own string function library. The function name is preceded by "mb _", in addition to the functions of the original function, these functions also add an optional $ encoding parameter at the end of the optional parameter. This parameter can specify the encoding method used by the function to process the string.

For example, the strpos () function finds the position of a string in another string.

Strpos ("Welcome to access", "Ask", 0) returns 12 because the script is UTF-8 encoded, and after converting the string into UTF-8 encoding, each Chinese Character occupies 3 bytes.

In the mb_strpos () function, mb_strpos ("Welcome to access", "Ask", 0, 'utf-8') returns 4, it executes the string as a State that has been converted to a UTF-8.

While mb_strpos ("welcome to visit", "Ask", 0, 'gbk') will return 6

Of course, it has more features ~

The following describes how to enable PHP Mb_String in Windows.

A few days ago, I ran a Php program and needed to convert it to character encoding. However, I did not support the Mb_String extension on the server. I checked that the Php extension library contains the php_mbstring.dll file.

The following describes how to enable it.

1. Make sure that you have the php_mbstring.dll file in Windows/system32. If not, just export it to Windows/system32 from the extensions directory of your Php installation directory.

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.