Table of Contents
你的php网页乱码了吗
Home php教程 php手册 你的php网页乱码了吗

你的php网页乱码了吗

Jun 13, 2016 am 08:59 AM
php one Garbled characters ? exist study of write Web page design

你的php网页乱码了吗

   一、

  学习php的童鞋在编写网页的时候,要是设计到中文内容的储存的时候,大多会出现一个问题就是乱码。一般乱码的话,我们可以检查三个方面

  (1)网页编码是否正确,比如是否在头部那里加入原标签

  meta charset="UTF-8">

  (2)检查mysql数据库储存时默认使用的字符集

  (3)检查网页文件的编码,是否为对应的中文编码

  ------------------

  环境:win7+wamp2.2

  ------------------

  二、

  下面我们就第二点开始,mysq数据库使用的编码字符集

  (1)打开mysql终端,查看当前的设置,确定要修改的范围

  show variables like 'char%';

  (2)根据结果进行分析,

  1、如果你显示的结果和我的差不多,即(只有character_set_system编码为utf8)那么清跟随下面的脚步一步步来

 

  2、打开my.ini文件,搜索关键词'mysqld',找到之后,观察是否有这一行

  character_set_server = utf8

  倘若没有的话,则应该像我这样在其下面添加一句

 

  3、再次搜索关键词'client',观察是否有这一行

  default_character_set = utf8

  没有的话在[client]下面添加上

 

  4、保存,重启mysql服务,关闭mysql终端(不然看到的客户端编码不会更新)

  5、再次打开终端,我们再次输入

  show variables like 'char%';

  倘若出现下面的结果,即mysql数据设置成功

 

  三、

  网页文件编码这个问题最容易被忽略,这个是在保存的时候,选择保存文件编码的格式时设置的。

  解决办法:

  1、使用notepad++打开该网页文件,然后在“格式”--“转为UTF-8无BOM编码格式”

 

  2、保存一下就好了

  问题分析:

  1、我在编写php的时候已经使用过

  "font-size:18px;">'set names utf8'); ?>

  但是还是出现乱码问题!

  分析:使用上面语句,修改的只是三项,这三项分别为

  character_set_client

  character_set_connection

  character_set_results

  而且这种修改只是临时性的,关闭终端后又和原来一样

  图片说明:

 

  2、我们对于第三个乱码问题进行分析一下

  (1)使用sublime text3编辑打开php文件。可以看到左下角有编码的信息,可以看到,目前的编码为gb2312

 

  (2)当网页加载的时候,那时也就相当于打开一个文件,那时读取的格式时按照gb2312的编码来读取网页文件,而在用户浏览器显示时,因为网页声明了字符集为utf-8,所以会将得到文件按utf-8字符集来解释内容,这是就会出现乱码了,而我们那些从数据库读取出来的内容却没有问题

  网页编码

 

  原来的gbk编码的文件

 

  后来utf-8编码的文件

 

  (3)注意一点,sublime text3 的转换编码的时候不太给你,虽然显示转换成功,可是呢?显示还是依旧,还是我们的notepad++给力一点,怎么修改前面有!转换成功之后

 

  3、为什么我按照你所说的修改了,在mysql终端下显示,还是乱码呢?

  分析:

  (1)我们先来看看windows下的cmd的使用的字符集是什么?

 

  可以看到,cmd的字符集是gbk,也就是说任何在终端显示的内容会以对应gbk这个字符集,但是我们数据库的中文设置的utf8这个字符集,当我们要显示时,自然会以utf8编码的读取数据库数据,那时编码为utf-8,一到终端就乱套了

  (2)那该怎么查看呢?

  使用phpmyadmin就好了打,当然得设置我们使用的utf-8编码!

        :更多精彩教程请关注帮客之家图文教程 频道,

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1658
14
PHP Tutorial
1257
29
C# Tutorial
1231
24
How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

See all articles