Home Backend Development PHP Tutorial PHP character encoding conversion problem

PHP character encoding conversion problem

Jul 25, 2016 am 08:53 AM

  1. $string=mb_convert_encoding($string, character output encoding, original character encoding);
Copy code

For example: the web page uses gbk encoding, the string uses utf-8 encoding, convert the characters to Web page display encoding.

  1. echo mb_convert_encoding("php encoding conversion", "gbk", "utf-8");
Copy code

Summary: It is recommended to use the mb_convert_encoding function, which has no iconv conversion character error problem.

Convert the encoding of the string to UTF-8. If you know the encoding of the original string, you can use the iconv function that everyone is familiar with. But now you don’t know the encoding of the original string. It may be GBK, or it may be UTF-8, so you cannot use this method. It would be great if you could get the string encoding type, or have an encoding conversion function that automatically recognizes the original string encoding. It turns out that the mb_convert_encoding function is the function that can automatically identify the original string encoding. However, during use, it was found that some Chinese characters in GBK were converted into garbled characters.

Later I found the is_utf8 function in the PHP manual, and then combined with the iconv function, the problem was solved.

Encoding conversion function used:

  1. <?php
  2. function is_utf8($string) {
  3. return preg_match('%^(?:
  4. [x09x0Ax0Dx20-x7E] # ASCII
  5. | [xC2-xDF][x80-xBF] # non -overlong 2-byte
  6. | xE0[xA0-xBF][x80-xBF] # excluding overlongs
  7. | [xE1-xECxEExEF][x80-xBF]{2} # straight 3-byte
  8. | x80-xBF] # excluding surrogates
  9. | xF4[x80-x8F][x80-xBF]{2} # plane 16
  10. )*$%xs', $string);
  11. } // function is_utf8
Copy code
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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

11 Best PHP URL Shortener Scripts (Free and Premium) 11 Best PHP URL Shortener Scripts (Free and Premium) Mar 03, 2025 am 10:49 AM

11 Best PHP URL Shortener Scripts (Free and Premium)

Working with Flash Session Data in Laravel Working with Flash Session Data in Laravel Mar 12, 2025 pm 05:08 PM

Working with Flash Session Data in Laravel

Build a React App With a Laravel Back End: Part 2, React Build a React App With a Laravel Back End: Part 2, React Mar 04, 2025 am 09:33 AM

Build a React App With a Laravel Back End: Part 2, React

Simplified HTTP Response Mocking in Laravel Tests Simplified HTTP Response Mocking in Laravel Tests Mar 12, 2025 pm 05:09 PM

Simplified HTTP Response Mocking in Laravel Tests

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

cURL in PHP: How to Use the PHP cURL Extension in REST APIs

12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

12 Best PHP Chat Scripts on CodeCanyon

Notifications in Laravel Notifications in Laravel Mar 04, 2025 am 09:22 AM

Notifications in Laravel

Announcement of 2025 PHP Situation Survey Announcement of 2025 PHP Situation Survey Mar 03, 2025 pm 04:20 PM

Announcement of 2025 PHP Situation Survey

See all articles