Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve php string garbled

2025-02-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "how to solve php string garbled". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Solutions to php string garbled: 1. Replace gb2312 with utf-8;2 through iconv, and transform coding through mb_convert_encoding function.

This article operating environment: Windows7 system, PHP7.1 version, DELL G3 computer

How to solve the garbled php string? The problem of character Encoding conversion of PHP iconv () function

In php, iconv function library can complete the conversion between various character sets, which is an indispensable basic function library in php programming; but sometimes iconv transcodes part of the data for no reason. For example, there will be errors when converting the character "-" to gb2312.

Let's take a look at the usage of this function.

For the simplest application, replace gb2312 with utf-8:

$text=iconv ("GB2312", "UTF-8", $text)

In the process of using $text=iconv ("UTF-8", "GB2312", $text), if you encounter some special characters, such as "-", "in the English name." Wait for the character, the conversion will be broken. The text after these characters can no longer be converted.

To solve this problem, you can use the following code:

$text=iconv ("UTF-8", "GBK", $text)

You read it correctly. It's as simple as that. Instead of using gb2312, you can write it as GBK.

In another way, the second parameter, plus / / IGNORE, ignores the error, as follows:

Iconv ("UTF-8", "GB2312//IGNORE", $data)

There is no specific comparison between the two methods, so the first method (GBK instead of gb2312) feels better.

Iconv () in the php manual states:

Iconv (PHP 4 > = 4.0.5, PHP 5) iconv-Convert string to requested character encodingDescriptionstring iconv (string in_charset, string out_charset, string str) Performs a character set conversion on the string str from in_charset to out_charset. Returns the converted string or FALSE on failure.If you append the string / / TRANSLIT to out_charset transliteration is activated. This means that when a character can't be represented in the target charset, it can be approximated through one or several similarly looking characters. If you append the string / / IGNORE, characters that cannot be represented in the target charset are silently discarded. Otherwise, str is cut from the first illegal character.

When using this function for string encoding conversion, it is important to note that if you convert utf-8 to gb2312, the string may be truncated. At this point, you can use the following methods:

$str=iconv ('utf-8', "gb2312//TRANSLIT", file_get_contents ($filepath))

That is, a red word is added to the second parameter, which means that if a character matching the source encoding is not found in the target encoding, a similar character will be selected for conversion. You can also use the parameter: / / IGNORE here to ignore characters that cannot be converted.

Ignore means to ignore the conversion error, and without the ignore parameter, all strings following that character cannot be saved.

Iconv is not the default function of php, but also the default installed module. It needs to be installed to use it.

If it is windows2000+php, you can modify the php.ini file to remove the ";" before extension=php_iconv.dll, and you want to copy the iconv.dll in your original php installation file to your winnt/system32 (if your dll points to this directory). In linux environment, using static installation, add one more item to configure-with-iconv, phpinfo can see the item of iconv. (Linux7.3+Apache4.06+php4.3.2).

Introduction of mb_convert_encoding and iconv functions

The function mb_convert_encoding is used to transform the code. I didn't understand the concept of program coding all the time, but now I seem to be a little enlightened. However, there is generally no coding problem in English, but only in Chinese data. For example, when you write a program with Zend Studio or Editplus, you use gbk coding. If the data needs to be entered into the database, and the database code is utf8, then the data should be encoded and converted, otherwise entering the database will become garbled.

Make a GBK To UTF-8:

And a GB2312 To Big5:

However, to use the above functions need to be installed, but you need to enable mbstring the extension library first.

String mb_convert_encoding (string str, string to_encoding [, mixed from_encoding]) needs to extend the library first by enable mbstring, which can specify multiple input codes without mb_convert_encoding in front of extension=php_mbstring.dll in php.ini. It will automatically identify according to the content, but the execution efficiency is much lower than iconv.

String iconv (string in_charset, string out_charset, string str) Note: the second parameter, in addition to specifying the encoding to be converted, you can also add two suffixes: / / TRANSLIT and / / IGNORE, in which / / TRANSLIT will automatically turn characters that cannot be converted directly into one or more similar characters, / / IGNORE will ignore unconvertible characters, and the default effect is truncation from the first illegal character.

In general, iconv is used, and the mb_convert_encoding function is used only when it is impossible to determine what the original code is, or when it cannot be displayed properly after iconv conversion.

$content = iconv ("GBK", "UTF-8", $content); $content = mb_convert_encoding ($content, "UTF-8", "how to solve php string garbled" is introduced here, thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report