声明:本篇文章来自http://www.jb51.net/article/30123.htm
PHP页面转UTF-8编码问题
1.在代码开始出加入一行:
header("Content-Type: text/html;charset=utf-8");
<span style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;">2.PHP文件编码问题 </span><br style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;"><span style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;">点击编辑器的菜单:“文件”->“另存为”,可以看到当前文件的编码,确保文件编码为:UTF-8, </span><br style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;"><span style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;">如果是ANSI,需要将编码改成:UTF-8。 </span><br style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;"><span style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;">3.PHP文件头BOM问题: </span><br style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;"><span style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;">PHP文件一定不可以有BOM标签 </span><br style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;"><span style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;">否则,会出现session不能使用的情况,并有类似的提示: </span><br style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;"><span style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;">Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent </span><br style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;"><span style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;">这是因为,在执行session_start() 的时候,整个页面不能有输出,但是当由于前PHP页面存在BOM标签, </span><br style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;"><span style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;">PHP把这个BOM标签当成是输出了,所以就出错了! </span><br style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;"><span style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;">所以PHP页面一定要删除BOM标签 </span><br style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;"><span style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;">删除这个BOM标签的方法: </span><br style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;"><span style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;">1.可以用Dreamweaver打开文件,并重新保存,即可以去除BOM标签! </span><br style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;"><span style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;">2.可以用EditPlus打开文件,并在菜单“首选项”->“文件”->"UTF-8标识",设置为:“总是删除签名”, </span><br style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;"><span style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;">然后保存文件,即可以去除BOM标签! </span><br style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;"><span style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;">4.PHP以附件形式保存文件的时候,UTF-8编码问题: </span><br style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;"><span style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;">PHP以附件形式保存文件,文件名必须是GB2312编码, </span><br style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;"><span style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;">否则,如果文件名中有中文的话,将是显示乱码: </span><br style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;"><span style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;">如果你的PHP本身是UTF-8编码格式的文件, </span><br style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;"><span style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;">需要将文件名变量由UTF-8转成GB2312: </span><br style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;"><span style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;">iconv("UTF-8", "GB2312", "$filename"); </span><br style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;"><span style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;">利用程序来实例字符截取方法 </span>
<span style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;"></span><pre name="code" class="php">function utf8_substr($str,$len) { for($i=0;$i 127){ $i++; if($i MYSQL数据库使用UTF-8编码的问题 <br> <br> 1.用phpmyadmin创建数据库和数据表 <br> 创建数据库的时候,请将“整理”设置为:“utf8_general_ci” <br> 或执行语句: <p></p> <pre name="code" class="sycode"><span style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;"></span><pre name="code" class="php">CREATE DATABASE `dbname` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
<span style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;">创建数据表的时候:如果是该字段是存放中文的话,则需要将“整理”设置为:“utf8_general_ci”, </span><br style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;"><span style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;">如果该字段是存放英文或数字的话,默认就可以了。 </span><br style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;"><span style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;">相应的SQL语句,例如: </span>
<span style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;"></span><pre name="code" class="sql">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.用PHP读写数据库
在连接数据库之后:
<span style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;"></span><pre name="code" class="php">$connection = mysql_connect($host_name, $host_user, $host_pass);加入两行:
<span style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;"><span style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;"></span></span><pre name="code" class="php">mysql_query("set character set 'utf8'");//读库 mysql_query("set names 'utf8'");//写库 //其实读写都可以只加入mysql_query("set names 'utf8'")
就可以正常的读写MYSQL数据库了。
用的appserv-win32-2.5.10做的环境,装这个包的时候用默认的utf8编码。
在写数据库连接文件时,写成:
<span style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;"><span style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;"><span style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;"></span></span></span><pre name="code" class="php">$conn = mysql_connect("$host","$user","$password"); mysql_query("SET NAMES 'UTF8'"); mysql_select_db("$database",$conn);然后在做页面时,注意这句:
<span style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;"><span style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;"><span style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;"><span style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px; background-color: rgb(221, 237, 251);"><meta http-equiv="Content-Type" content="text/html; charset=utf-8"> </span></span></span></span>
<span style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;"><span style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;"><span style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;"><span style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;">这样不管输入数据库的中文,还是页面显示,就都正常了。 </span><br style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;"><span style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;">在DW CS4版里,默认生成的也是utf8页面。 </span><br style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;"><span style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;">同样的,如果一开始写数据库连接文件时写成: </span></span></span></span><pre name="code" class="php">mysql_query("SET NAMES 'GBK'");那页面也要相应变成:
<span style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;"><span style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;"><span style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;"></span></span></span><pre name="code" class="cpp"><meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<span style="font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px;"></span>

長URL(通常用關鍵字和跟踪參數都混亂)可以阻止訪問者。 URL縮短腳本提供了解決方案,創建了簡潔的鏈接,非常適合社交媒體和其他平台。 這些腳本對於單個網站很有價值

在Facebook在2012年通過Facebook備受矚目的收購之後,Instagram採用了兩套API供第三方使用。這些是Instagram Graph API和Instagram Basic Display API。作為開發人員建立一個需要信息的應用程序

Laravel使用其直觀的閃存方法簡化了處理臨時會話數據。這非常適合在您的應用程序中顯示簡短的消息,警報或通知。 默認情況下,數據僅針對後續請求: $請求 -

這是有關用Laravel後端構建React應用程序的系列的第二個也是最後一部分。在該系列的第一部分中,我們使用Laravel為基本的產品上市應用程序創建了一個RESTFUL API。在本教程中,我們將成為開發人員

Laravel 提供简洁的 HTTP 响应模拟语法,简化了 HTTP 交互测试。这种方法显著减少了代码冗余,同时使您的测试模拟更直观。 基本实现提供了多种响应类型快捷方式: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

PHP客戶端URL(curl)擴展是開發人員的強大工具,可以與遠程服務器和REST API無縫交互。通過利用Libcurl(備受尊敬的多協議文件傳輸庫),PHP curl促進了有效的執行

您是否想為客戶最緊迫的問題提供實時的即時解決方案? 實時聊天使您可以與客戶進行實時對話,並立即解決他們的問題。它允許您為您的自定義提供更快的服務

2025年的PHP景觀調查調查了當前的PHP發展趨勢。 它探討了框架用法,部署方法和挑戰,旨在為開發人員和企業提供見解。 該調查預計現代PHP Versio的增長


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境