Home Web Front-end CSS Tutorial Why is html garbled and how to solve it

Why is html garbled and how to solve it

Nov 21, 2017 pm 05:03 PM
html Why solve

There are three reasons that cause html web pages to be garbled. So today we will talk about three different causes and solutions to the three problems.

The cause of garbled HTML web pages is mainly caused by the difference between the Chinese text content in the HTML source code and the HTML encoding. But no matter which situation causes garbled code, the web page encoding needs to be set at the beginning of the web page.

Causes of garbled codes

1. For example, the source code of the web page is encoded by gbk, and the Chinese characters in the content are encoded by utf-8. In this case, html garbled codes will appear when the browser is opened. On the contrary, if the web page is encoded in utf-8 and the content is gbk, garbled characters will appear.

2. The encoding of the html web page is gbk, and the program calls out the content encoded in utf-8 from the database, which will also cause encoding garbled characters.

3. The browser cannot automatically detect the encoding of the web page, causing the web page to be garbled.


Methods to solve garbled characters

The first one is that the source code encoding of the html web page is different from the Chinese character input encoding.

Solution:

Use software to edit HTML web page content. It is recommended to use DW software for HTML code editing and development.

Try not to use Notepad directly to edit HTML code.


Second, if the web page setting encoding is gbk, and the database storage data encoding format is UTF-8, at this time the program queriesthe database The data display data can be transcoded before entering the local program.

For example, PHP program + mysql query display data transcoding:

1, mysql_query("SET NAMES 'UTF8'"); //Transcode the query data to utf8, that is, converted to utf-8

2, mysql_query("SET NAMES 'GBK'");//Transcode the query data to GBK, such as gbk2312

when writing the database connection When writing the file, write:

$conn = mysql_connect("$host","$user","$password"); 
mysql_query("SET NAMES 'UTF8'"); 
mysql_select_db("$database",$conn);
Copy after login

Then when making the page, pay attention to this sentence:

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
Copy after login

In this way, regardless of the Chinese input into the database or the page display, everything will be normal.

In the DW CS4 version, the utf8 page is also generated by default.

Similarly, if you write the database connection file at the beginning:

mysql_query("SET NAMES 'GBK'");

Then the page should also become:

<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
Copy after login

OthersASP programs or other programming language websites can check the conversion encoding method on Baidu according to the actual situation.


The third type is that the browser causes garbled characters.

This may be caused by your webpage not setting meta charset encoding. As a result, the browser cannot recognize the default encoding type of your web page. Solution:

1. If the web page is garbled when browsing in the browser, find the menu to convert the encoding in the browser.

In IE9 browser, right-click the blank page of the webpage that needs to be transcoded and select "Encoding".

When browsing web pages that need to be transcoded in Maxthon Browser, menu "View"-->"Encoding" to select the conversion encoding

When browsing web pages that need to be transcoded in Google Chrome , click the "three horizontal" icon in the upper right corner and select "Tools" --> "Encoding" to choose to switch the web page encoding so that the browser will not browse this web page with garbled characters.

There are so many reasons and solutions for garbled HTML web pages. Friends who need it can save it. Please also continue to pay attention to other updates of this site.

Related reading:

How to use label tags in html


HTML title tag element How to modify


Tips for using CSS to manipulate borders

The above is the detailed content of Why is html garbled and how to solve it. For more information, please follow other related articles on the PHP Chinese website!

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)

React's Role in HTML: Enhancing User Experience React's Role in HTML: Enhancing User Experience Apr 09, 2025 am 12:11 AM

React combines JSX and HTML to improve user experience. 1) JSX embeds HTML to make development more intuitive. 2) The virtual DOM mechanism optimizes performance and reduces DOM operations. 3) Component-based management UI to improve maintainability. 4) State management and event processing enhance interactivity.

Understanding HTML, CSS, and JavaScript: A Beginner's Guide Understanding HTML, CSS, and JavaScript: A Beginner's Guide Apr 12, 2025 am 12:02 AM

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

The Role of HTML: Structuring Web Content The Role of HTML: Structuring Web Content Apr 11, 2025 am 12:12 AM

The role of HTML is to define the structure and content of a web page through tags and attributes. 1. HTML organizes content through tags such as , making it easy to read and understand. 2. Use semantic tags such as, etc. to enhance accessibility and SEO. 3. Optimizing HTML code can improve web page loading speed and user experience.

HTML: The Structure, CSS: The Style, JavaScript: The Behavior HTML: The Structure, CSS: The Style, JavaScript: The Behavior Apr 18, 2025 am 12:09 AM

The roles of HTML, CSS and JavaScript in web development are: 1. HTML defines the web page structure, 2. CSS controls the web page style, and 3. JavaScript adds dynamic behavior. Together, they build the framework, aesthetics and interactivity of modern websites.

How to recover data after SQL deletes rows How to recover data after SQL deletes rows Apr 09, 2025 pm 12:21 PM

Recovering deleted rows directly from the database is usually impossible unless there is a backup or transaction rollback mechanism. Key point: Transaction rollback: Execute ROLLBACK before the transaction is committed to recover data. Backup: Regular backup of the database can be used to quickly restore data. Database snapshot: You can create a read-only copy of the database and restore the data after the data is deleted accidentally. Use DELETE statement with caution: Check the conditions carefully to avoid accidentally deleting data. Use the WHERE clause: explicitly specify the data to be deleted. Use the test environment: Test before performing a DELETE operation.

HTML: Building the Structure of Web Pages HTML: Building the Structure of Web Pages Apr 14, 2025 am 12:14 AM

HTML is the cornerstone of building web page structure. 1. HTML defines the content structure and semantics, and uses, etc. tags. 2. Provide semantic markers, such as, etc., to improve SEO effect. 3. To realize user interaction through tags, pay attention to form verification. 4. Use advanced elements such as, combined with JavaScript to achieve dynamic effects. 5. Common errors include unclosed labels and unquoted attribute values, and verification tools are required. 6. Optimization strategies include reducing HTTP requests, compressing HTML, using semantic tags, etc.

HTML and Code: A Closer Look at the Terminology HTML and Code: A Closer Look at the Terminology Apr 10, 2025 am 09:28 AM

HTMLisaspecifictypeofcodefocusedonstructuringwebcontent,while"code"broadlyincludeslanguageslikeJavaScriptandPythonforfunctionality.1)HTMLdefineswebpagestructureusingtags.2)"Code"encompassesawiderrangeoflanguagesforlogicandinteract

Centos stops maintenance 2024 Centos stops maintenance 2024 Apr 14, 2025 pm 08:39 PM

CentOS will be shut down in 2024 because its upstream distribution, RHEL 8, has been shut down. This shutdown will affect the CentOS 8 system, preventing it from continuing to receive updates. Users should plan for migration, and recommended options include CentOS Stream, AlmaLinux, and Rocky Linux to keep the system safe and stable.

See all articles