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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Two Point Museum: All Exhibits And Where To Find Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

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)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

See all articles