php method to solve the garbled WeChat nickname: 1. Use decodeURIComponent to decode; 2. Database encoding, table encoding and corresponding storage fields use utf8mb4 encoding.
The operating environment of this article: macOS10.15 system, PHP7.1 version, macbook air2017 computer
How does php solve the problem of garbled WeChat nicknames?
The first solution to solve the garbled Emoji expression
Encode the information containing the Emoji expression into encodeURIComponent and decode it in decodeURIComponent where it is used. This solution has been I personally tested it in the mini program "Garbage Classification Blackboard Report", as shown in the figure below:
This solution is not good. It only solves the problem of garbled characters and brings The additional encoding and decoding overhead of the code is eliminated.
The second solution to solve the garbled Emoji expressions
According to my description using Mysql database and PHP YII2 framework
Two steps
Use utf8mb4 when connecting to the database. This is very important and easy to miss.
Database encoding, table encoding and corresponding storage fields should all use utf8mb4 encoding
The code example for the first step is as follows
This solution is available for personal testing in the mini program "Group Blackboard".
This solution is the most perfect solution, which does not bring additional code expenses. It is also the recommended solution that can be found online. However, in the actual operation process, Sometimes it may not take effect due to database version or personal settings, but this solution is really good.
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to solve the problem of garbled WeChat nickname in PHP. For more information, please follow other related articles on the PHP Chinese website!