Learn the conversion and judgment of PHP string encoding

WBOY
Release: 2016-07-25 08:56:22
Original
837 people have browsed it
This article introduces a piece of code for transcoding and judging string encoding in PHP programming. Friends in need can use it as a reference. It is often necessary to implement GBK and UTF-8 encoding conversion in PHP. For example, json_encode in PHP itself does not support GBK encoding at all. There are two library functions that can support encoding conversion. The one that usually comes to mind is the iconv function. For example:

            
Copy after login
But iconv can only solve the situation where the encoding is known in advance. If the string encoding is unknown, you need to detect its encoding first. In this case, the mb_string extension library may be used:

            
Copy after login
However, mb_detect_encoding has a flaw, and inaccurate judgment often occurs. You can refer to the following solutions:

            
Copy after login
After obtaining the string encoding information through the above method, you can use iconv or mb_convert_encoding to perform encoding conversion.


source:php.cn
Statement of this Website
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!