SoFunction
Updated on 2025-03-08

How to solve the problem of garbled Chinese reply on WeChat public platform

This article analyzes the solution to the problem of garbled code in Chinese reply to the php version of WeChat public platform. Share it for your reference, as follows:

During the development of WeChat public, I encountered a garbled response in Chinese. The editor found that this problem was a coding problem. In fact, just convert the encoding to utf8 to solve it. Let’s take a look.

Many automatic reply programs on WeChat public platforms are developed in this category. Today I encountered an inexplicable garbled problem. I found that it was caused by GB2312 encoding, so I had to modify the source code.

Add one method first:

/**
  * Detect whether UTF-8 is detected
  * @param $str
  * @return bool
  */
private function is_utf8($str)
{
  return preg_match('//u', $str);
}
//turn up$this->data ['Content'] = $content;
//Modify toif ($this->is_utf8($content)) {
  $this->data ['Content'] = $content;
} else {
  $this->data ['Content'] = iconv('gb2312', 'UTF-8//IGNORE', $content);
}
//Enjoy

For more information about PHP related content, please check out the topic of this site:Summary of PHP WeChat development skills》、《Summary of PHP encoding and transcoding operation techniques》、《Summary of PHP network programming skills》、《Introduction to PHP basic syntax》、《Summary of usage of php strings》、《PHP+mysql database operation tutorial"and"Summary of common database operation techniques for php

I hope this article will be helpful to everyone's PHP programming.