Solution to the problem of garbled characters in Chinese replies on the PHP version of WeChat public platform

高洛峰
Release: 2023-03-04 19:32:01
Original
1618 people have browsed it

This article analyzes the solution to the problem of garbled Chinese characters in 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, the reply encountered garbled Chinese characters. 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 the WeChat public platform are developed by the ThinkWechat.class.php class. Today I encountered an inexplicable garbled 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);
}
//即可
Copy after login

##I hope this article will be helpful to everyone in PHP programming.

For more related articles on how to solve the problem of garbled characters in Chinese replies on the PHP version of WeChat public platform, please pay attention to the PHP Chinese website!

Related labels:
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template