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 the problem of garbled codes in PHP

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

What this article shares with you is about how to solve the problem of PHP Chinese garbled code. The editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

Garbled Chinese characters is really a sad thing. JAVA hates Chinese characters, and PHP doesn't like Chinese characters either.

Java garbled is eventually filtered using the filter given by spring, which actually affects the speed, but there is no way. Chinese characters are the first thing that W does not consider.

Unexpectedly, PHP is also a place of garbled code. When you use your brother MySQL, the Chinese character looks so friendly that you never thought it would become a book of heaven. However, in order to interact with others, when you extend PHP's hand to SQL SERVER, the garbled code comes because of the GBK code used by the third-party system.

Hey, let's switch.

ICONV, a high-end function, is included with PHP.

String iconv (string $in_charset, string $out_charset, string $str)

Use DEMO:

We all recommend the function, but after the use of no conversion, no errors, no character conversion, NO!

2, another way, there is another function that people question about inefficiency, but in any case, implement it first and then consider the other three.

/ / check whether the function is available with echo function_exists ('mb_convert_encoding'); / / detect the current encoding echo mb_detect_encoding ($val, "GBK, GB2312, UTF-8"); / / convert the code, converting CP936 (that is, GBK) to UTF-8$ v=mb_convert_encoding ($val, "UTF-8", "CP936")

It turned out to be successful.

All right, let's use it first. to convert the result set of a database query, make a conversion function:

1, the function "garbled nemesis":

/ / $fContents string / / Encoding of $from string / / $to Encoding function auto_charset ($fContents,$from='gbk',$to='utf-8') {$from= strtoupper ($from) = = 'UTF8'? 'utf-8':$from; $to = strtoupper ($to) = = 'UTF8'? 'utf-8':$to; if (strtoupper ($from) = strtoupper ($to) | | empty ($fContents) | | (is_scalar ($fContents) & &! is_string ($fContents)) {/ / return $fContents is not converted if the encoding is the same or is not a string scalar } if (is_string ($fContents)) {if (function_exists ('mb_convert_encoding')) {return mb_convert_encoding ($fContents, $to, $from);} else {return $fContents;}} elseif (is_array ($fContents)) {foreach ($fContents as $key = > $val) {$_ key = auto_charset ($key,$from,$to) $fContents [$_ key] = auto_charset ($val,$from,$to); if ($key! = $key) unset ($fContents [$key]);} return $fContents;} else {return $fContents;}}

2, use:

/ / print out the query results (assuming your results) $arr=array (); while ($list=mssql_fetch_row ($row)) {$arr [] = $list;} $s=auto_charset ($arr,'gbk','utf-8'); / / try printing. Set the code to UFT-8 in the browser to see no garbled print_r ($s); die () The above is how to solve the problem of Chinese garbled code in PHP. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

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

Servers

Wechat

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

12
Report