Home  >  Article  >  Backend Development  >  php sets string encoding format

php sets string encoding format

藏色散人
藏色散人Original
2020-08-13 09:45:333923browse

The way to set the string encoding format in php is to add a line of code at the beginning of the code as "header("Content-Type: text/html;charset=utf-8");".

php sets string encoding format

Recommended: "PHP Video Tutorial"

php Set the character encoding to utf-8

Add a line at the beginning of the code:

header("Content-Type: text/html;charset=utf-8");

Related introduction:

The essence of PHP strings

Explanation quoted from PHP documentation:

The implementation of string in PHP is an array of bytes plus an integer indicating the buffer length. There is no information on how to convert bytes into characters, it is up to the programmer to decide. There are no restrictions on what values ​​a string consists of, including bytes with a value of 0 that can appear anywhere in the string.

PHP does not specify the encoding of the string. How the string is encoded depends on the programmer. Strings are encoded according to the encoding of the PHP file. For example, if your file encoding is GBK, then all your code content is GBK.

Supplementary to the concept of binary safety, a byte with a value of 0 (NULL) can be at any position in the string, and the bottom layer of some non-binary functions of PHP is the C function called, which will ignore the characters after NULL .

As long as PHP's file encoding is compatible with ASCII, string operations can be processed well. However, string operations are still Native in nature (no matter what the file encoding is), so you need to pay attention when using it:

Some functions assume that strings are encoded in single bytes, but they do not need to be Bytes are interpreted as specific characters. For example, the sbustr() function.

Many functions need to pass encoding parameters explicitly, otherwise the default value will be obtained from the PHP.INI file, such as the htmlentities() function.

There are also some functions related to the local area, and these functions can only operate on single bytes.

Under normal circumstances, although PHP does not support Unicode characters internally, it does support UTF-8 encoding. In most cases, there will be no problem, but the following situations may not be handled:

How to convert non-UTF-8 encoded strings

A UTF-8 encoded web page, but when users submit the form, they may use GBK encoding (does not comply with meta tags)

For a UTF-8 encoded PHP file, using strlen("China") returns 6 instead of the actual number of characters (2)

So how to solve this problem? PHP provides the mbstring extension!

The above is the detailed content of php sets string encoding format. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn