亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

Home Backend Development PHP Problem How to change the encoding of php garbled code

How to change the encoding of php garbled code

Sep 14, 2021 am 09:46 AM
php Garbled characters

How to change the encoding of php garbled code: 1. Add a line "" to the HTML page; 2. Add a line "header("Content -Type: text/html;charset=utf-8");". =utf-8'>

How to change the encoding of php garbled code

The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer

php garbled code How to change the encoding?

PHP garbled code problem, UTF-8 (garbled code)

1. HTML page conversion to UTF-8 encoding problem

1. Add a line after the head and before the title:

<meta http-equiv=&#39;Content-Type&#39; content=&#39;text/html; charset=utf-8&#39; />

The order must not be wrong, the title displayed in

may be garbled!

2.html file encoding problem:

Click the menu of the editor: "File"->"Save As", you can see the encoding of the current file, make sure the file encoding is: UTF -8,

If it is ANSI, you need to change the encoding to: UTF-8.

3. HTML file header BOM problem:

When converting a file from other encodings to UTF-8 encoding, sometimes a BOM tag is added at the beginning of the file,

A BOM tag may cause the browser to display garbled characters when displaying Chinese characters.

How to delete this BOM tag:

1. You can open the file with Dreamweaver and resave it to remove the BOM tag!

2. You can open the file with EditPlus, and in the menu "Preferences"->"File"->"UTF-8 Identity", set it to: "Always delete signature",

Then save the file and you can remove the BOM tag!

4. WEB server UTF-8 encoding problem:

If you follow the steps listed above and still have Chinese garbled problems,

Please check your Encoding problem of the WEB server used

If you are using Apache, please set the charset in the configuration file to: utf-8 (only the methods are listed here, please refer to the apache configuration file for the specific format)

If you are using Nginx, please set the charset in nginx.conf to utf-8.

Specify find "charset gb2312;" or similar statement and change it to: " charset utf-8;".

2. PHP page conversion to UTF-8 encoding problem

1. Add a line at the beginning of the code:

header("Content-Type: text/html;charset=utf-8");

2. PHP file encoding problem

Click the editor's menu: "File" -> "Save As", you can see the encoding of the current file, make sure the file encoding is: UTF-8,

If it is ANSI, you need to change the encoding to: UTF-8.

3. PHP file header BOM problem:

PHP files must not have BOM tags

Otherwise, the session will not be used, and there will be a similar prompt:

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent

This is because, when executing session_start(), the entire page cannot be Output, but when the BOM tag exists in the previous PHP page,

PHP treats this BOM tag as output, so an error occurs!

So the PHP page must delete the BOM tag

How to delete this BOM tag:

1. You can open the file with Dreamweaver and resave it , that is, the BOM label can be removed!

2. You can open the file with EditPlus, and in the menu "Preferences"->"File"->"UTF-8 Identity", set it to: "Always delete signature",

Then save the file and you can remove the BOM tag!

4. UTF-8 encoding problem when PHP saves files as attachments:

When PHP saves files as attachments, the file name must be GB2312 encoded,

Otherwise , if there is Chinese in the file name, it will be garbled:

If your PHP itself is a file in UTF-8 encoding format,

You need to convert the file name variable from UTF-8 Into GB2312:

iconv("UTF-8", "GB2312", "$filename");

5. When the article title is truncated and displayed, garbled characters or "?" question marks appear:

Generally, when the article title is very long, part of the title will be displayed, which will affect the article. The title is truncated,

Since a Chinese character in UTF-8 encoding format will occupy 3 characters of width,

When intercepting the title, sometimes only 1 character of a Chinese character will be intercepted Or 2 characters wide,

is not completely intercepted, and garbled characters or "?" question marks will appear.

Use the following function to intercept the title, and there will be no problem:

function get_brief_str($str, $max_length) 
{ 
echo strlen($str) ."<br>"; 
if(strlen($str) > $max_length) 
{ 
$check_num = 0; 
for($i=0; $i < $max_length; $i++) 
{ 
if (ord($str[$i]) > 128) 
$check_num++; 
} 
if($check_num % 3 == 0) 
$str = substr($str, 0, $max_length)."..."; 
else if($check_num % 3 == 1) 
$str = substr($str, 0, $max_length + 2)."..."; 
else if($check_num % 3 == 2) 
$str = substr($str, 0, $max_length + 1)."..."; 
} 
return $str; 
}

3. Problems with using UTF-8 encoding for MYSQL database

1. Use phpmyadmin to create databases and data tables

When creating a database , please set "Organization" to: "utf8_general_ci"

Or execute the statement:

CREATE DATABASE `dbname` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;

When creating a data table: If the field stores Chinese, you need to set "Organization" Set to: "utf8_general_ci",

If the field stores English or numbers, the default is fine.

Corresponding SQL statement, for example:

CREATE TABLE `test` ( 
`id` INT NOT NULL , 
`name` VARCHAR( 10 ) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL , 
PRIMARY KEY ( `id` ) 
) ENGINE = MYISAM ;

2. Use PHP to read and write the database

After connecting to the database:

[hide]$connection = mysql_connect($host_name, $host_user, $host_pass);

Add two lines:

mysql_query("set character set &#39;utf8&#39;");//讀庫
mysql_query("set names &#39;utf8&#39;");//寫庫

就可以正常的讀寫MYSQL數(shù)據(jù)庫了。

四.JS相關(guān)的UTF-8編碼問題

1.JS讀Cookie的中文亂碼問題

PHP寫cookie的時(shí)候需要將中文字符進(jìn)行escape編碼,

否則JS讀到cookie中的中文字符將是亂碼。

但php本身沒有escape函數(shù),我們新寫一個(gè)escape函數(shù):

function escape($str) 
{ 
preg_match_all("/[\x80-\xff].|[\x01-\x7f]+/",$str,$r); 
$ar = $r[0]; 
foreach($ar as $k=>$v) 
{ 
if(ord($v[0]) < 128) 
$ar[$k] = rawurlencode($v); 
else 
$ar[$k] = "%u".bin2hex(iconv("UTF-8","UCS-2",$v)); 
} 
return join("",$ar); 
}

JS讀cookie的時(shí)候,用unescape解碼,

然后就解決cookie中有中文亂碼的問題了。

2.外部JS文件UTF-8編碼問題

當(dāng)一個(gè)HTML頁面或則PHP頁面包含一個(gè)外部的JS文件時(shí),

如果HTML頁面或則PHP頁面是UTF-8編碼格式的文件,

外部的JS文件同樣要轉(zhuǎn)成UTF-8的文件,

否則將出現(xiàn),沒有包含不成功,調(diào)用函數(shù)時(shí)沒有反應(yīng)的情況。

點(diǎn)擊編輯器的菜單:“文件”->“另存為”,可以看到當(dāng)前文件的編碼,確保文件編碼為:UTF-8,

如果是ANSI,需要將編碼改成:UTF-8。

五.FLASH相關(guān)的UTF-8編碼問題

FLASH內(nèi)部對所有字符串,默認(rèn)都是以UTF-8處理?

1.FLASH讀文普通本文件(txt,html)?

要將文本文件的編碼存為UTF-8?

點(diǎn)擊編輯器的菜單:“文件”->“另存為”,可以看到當(dāng)前文件的編碼,確保文件編碼為:UTF-8,?

如果是ANSI,需要將編碼改成:UTF-8。?

2.FLASH讀XML文件?

要將XML文件的編碼存為UTF-8?

點(diǎn)擊編輯器的菜單:“文件”->“另存為”,可以看到當(dāng)前文件的編碼,確保文件編碼為:UTF-8,?

如果是ANSI,需要將編碼改成:UTF-8。?

在XML第1行寫:?

3.FLASH讀PHP返回?cái)?shù)據(jù)?

如果PHP編碼本身是UTF-8的,直接echo就可以了?

如果PHP編碼本身是GB2312的,可以將PHP轉(zhuǎn)存成UTF-8編碼格式的文件,直接echo就可以了

如果PHP編碼本身是GB2312的,而且不允許改文件的編碼格式,?

用下面的語句將字符串轉(zhuǎn)換成UTF-8的編碼格式?

$new_str = iconv("GB2312", "UTF-8", "$str");?

再echo就可以了?

4.FLASH讀數(shù)據(jù)庫(MYSQL)的數(shù)據(jù)?

FLASH要通過PHP讀取數(shù)據(jù)庫中的數(shù)據(jù)?

PHP本身的編碼不重要,關(guān)鍵是如果數(shù)據(jù)庫的編碼是GB2312的話,?

需要用下面的語句將字符串轉(zhuǎn)換成UTF-8的編碼格式?

$new_str = iconv("GB2312", "UTF-8", "$str");?

5.FLASH通過PHP寫數(shù)據(jù)?

一句話,F(xiàn)LASH傳過來的字符串是UTF-8格式的,?

要轉(zhuǎn)換成相應(yīng)的編碼格式,再操作(寫文件、寫數(shù)據(jù)庫、直接顯示等等)?

還是用iconv函數(shù)轉(zhuǎn)換?

6.FLASH使用本地編碼(理論上不推薦使用)?

如果想讓FLASH不使用UTF-8編碼,而是使用本地編碼?

對于中國大陸地區(qū)而言,本地編碼是GB2312或GBK?

AS程序內(nèi),可以添加以下代碼:?

System.useCodepage = true;?

那么FLASH內(nèi)所有字符都是使用GB2312的編碼了?

所有導(dǎo)入到FLASH或者從FLASH導(dǎo)出的數(shù)據(jù),都應(yīng)該做相應(yīng)的編碼轉(zhuǎn)換?

因?yàn)槭褂帽镜鼐幋a,會造成使用繁體中文地區(qū)的用戶產(chǎn)生亂碼,所以不推薦使用

推薦學(xué)習(xí):《PHP視頻教程

The above is the detailed content of How to change the encoding of php garbled code. 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

Undress AI Tool

Undress AI Tool

Undress images for free

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.

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

PHP Tutorial
1488
72
PHP calls AI intelligent voice assistant PHP voice interaction system construction PHP calls AI intelligent voice assistant PHP voice interaction system construction Jul 25, 2025 pm 08:45 PM

User voice input is captured and sent to the PHP backend through the MediaRecorder API of the front-end JavaScript; 2. PHP saves the audio as a temporary file and calls STTAPI (such as Google or Baidu voice recognition) to convert it into text; 3. PHP sends the text to an AI service (such as OpenAIGPT) to obtain intelligent reply; 4. PHP then calls TTSAPI (such as Baidu or Google voice synthesis) to convert the reply to a voice file; 5. PHP streams the voice file back to the front-end to play, completing interaction. The entire process is dominated by PHP to ensure seamless connection between all links.

How to use PHP to build social sharing functions PHP sharing interface integration practice How to use PHP to build social sharing functions PHP sharing interface integration practice Jul 25, 2025 pm 08:51 PM

The core method of building social sharing functions in PHP is to dynamically generate sharing links that meet the requirements of each platform. 1. First get the current page or specified URL and article information; 2. Use urlencode to encode the parameters; 3. Splice and generate sharing links according to the protocols of each platform; 4. Display links on the front end for users to click and share; 5. Dynamically generate OG tags on the page to optimize sharing content display; 6. Be sure to escape user input to prevent XSS attacks. This method does not require complex authentication, has low maintenance costs, and is suitable for most content sharing needs.

How to use PHP combined with AI to achieve text error correction PHP syntax detection and optimization How to use PHP combined with AI to achieve text error correction PHP syntax detection and optimization Jul 25, 2025 pm 08:57 PM

To realize text error correction and syntax optimization with AI, you need to follow the following steps: 1. Select a suitable AI model or API, such as Baidu, Tencent API or open source NLP library; 2. Call the API through PHP's curl or Guzzle and process the return results; 3. Display error correction information in the application and allow users to choose whether to adopt it; 4. Use php-l and PHP_CodeSniffer for syntax detection and code optimization; 5. Continuously collect feedback and update the model or rules to improve the effect. When choosing AIAPI, focus on evaluating accuracy, response speed, price and support for PHP. Code optimization should follow PSR specifications, use cache reasonably, avoid circular queries, review code regularly, and use X

PHP creates a blog comment system to monetize PHP comment review and anti-brush strategy PHP creates a blog comment system to monetize PHP comment review and anti-brush strategy Jul 25, 2025 pm 08:27 PM

1. Maximizing the commercial value of the comment system requires combining native advertising precise delivery, user paid value-added services (such as uploading pictures, top-up comments), influence incentive mechanism based on comment quality, and compliance anonymous data insight monetization; 2. The audit strategy should adopt a combination of pre-audit dynamic keyword filtering and user reporting mechanisms, supplemented by comment quality rating to achieve content hierarchical exposure; 3. Anti-brushing requires the construction of multi-layer defense: reCAPTCHAv3 sensorless verification, Honeypot honeypot field recognition robot, IP and timestamp frequency limit prevents watering, and content pattern recognition marks suspicious comments, and continuously iterate to deal with attacks.

How to use PHP to combine AI to generate image. PHP automatically generates art works How to use PHP to combine AI to generate image. PHP automatically generates art works Jul 25, 2025 pm 07:21 PM

PHP does not directly perform AI image processing, but integrates through APIs, because it is good at web development rather than computing-intensive tasks. API integration can achieve professional division of labor, reduce costs, and improve efficiency; 2. Integrating key technologies include using Guzzle or cURL to send HTTP requests, JSON data encoding and decoding, API key security authentication, asynchronous queue processing time-consuming tasks, robust error handling and retry mechanism, image storage and display; 3. Common challenges include API cost out of control, uncontrollable generation results, poor user experience, security risks and difficult data management. The response strategies are setting user quotas and caches, providing propt guidance and multi-picture selection, asynchronous notifications and progress prompts, key environment variable storage and content audit, and cloud storage.

PHP realizes commodity inventory management and monetization PHP inventory synchronization and alarm mechanism PHP realizes commodity inventory management and monetization PHP inventory synchronization and alarm mechanism Jul 25, 2025 pm 08:30 PM

PHP ensures inventory deduction atomicity through database transactions and FORUPDATE row locks to prevent high concurrent overselling; 2. Multi-platform inventory consistency depends on centralized management and event-driven synchronization, combining API/Webhook notifications and message queues to ensure reliable data transmission; 3. The alarm mechanism should set low inventory, zero/negative inventory, unsalable sales, replenishment cycles and abnormal fluctuations strategies in different scenarios, and select DingTalk, SMS or Email Responsible Persons according to the urgency, and the alarm information must be complete and clear to achieve business adaptation and rapid response.

Beyond the LAMP Stack: PHP's Role in Modern Enterprise Architecture Beyond the LAMP Stack: PHP's Role in Modern Enterprise Architecture Jul 27, 2025 am 04:31 AM

PHPisstillrelevantinmodernenterpriseenvironments.1.ModernPHP(7.xand8.x)offersperformancegains,stricttyping,JITcompilation,andmodernsyntax,makingitsuitableforlarge-scaleapplications.2.PHPintegrateseffectivelyinhybridarchitectures,servingasanAPIgateway

PHP integrated AI speech recognition and translator PHP meeting record automatic generation solution PHP integrated AI speech recognition and translator PHP meeting record automatic generation solution Jul 25, 2025 pm 07:06 PM

Select the appropriate AI voice recognition service and integrate PHPSDK; 2. Use PHP to call ffmpeg to convert recordings into API-required formats (such as wav); 3. Upload files to cloud storage and call API asynchronous recognition; 4. Analyze JSON results and organize text using NLP technology; 5. Generate Word or Markdown documents to complete the automation of meeting records. The entire process needs to ensure data encryption, access control and compliance to ensure privacy and security.

See all articles