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!