Home  >  Article  >  Backend Development  >  Solution to the problem of garbled characters in Chinese replies on the PHP version of WeChat public platform

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

高洛峰
高洛峰Original
2017-01-14 13:44:021588browse

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);
}
//即可

##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!

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