Home >Backend Development >PHP Problem >How to solve the php garbled problem of WeChat public platform

How to solve the php garbled problem of WeChat public platform

藏色散人
藏色散人Original
2021-05-24 09:30:362680browse

Solution to WeChat public platform php garbled code: first open the corresponding source code file; then add the content as "private function is_utf8($str){return preg_match('//u', $str);}.. .” That’s it.

How to solve the php garbled problem of WeChat public platform

The operating environment of this article: Windows 7 system, PHP version 7.1, DELL G3 computer

php version WeChat public platform replies with garbled Chinese characters Solution

This article analyzes the solution to the problem of garbled characters in Chinese replies on the PHP version of WeChat public platform. Share it with everyone for your reference. The details are as follows:

When developing the WeChat public platform, I encountered garbled Chinese characters in the replies. The editor found that this problem was an encoding problem. In fact, it can be solved by converting the encoding to utf8. Specifically Let’s take a look.

Many automatic reply programs on WeChat public platforms are developed by the ThinkWechat.class.php class. Today I encountered an inexplicable garbled code problem. After checking the problem, I found that it was caused by GB2312 encoding, so I need to modify it. Source code.

First add a method:

/**
 * 检测是否UTF-8
 * @param $str
 * @return bool
 */
private function is_utf8($str)
{
  return preg_match('//u', $str);
}
//找到
$this->data ['Content'] = $content;
//修改为
if ($this->is_utf8($content)) {
  $this->data ['Content'] = $content;
} else {
  $this->data ['Content'] = iconv('gb2312', 'UTF-8//IGNORE', $content);
}
//即可

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to solve the php garbled problem of WeChat public platform. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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
Previous article:How to use round in phpNext article:How to use round in php