在任意字符集下正常顯示網(wǎng)頁(yè)的方法一
更新時(shí)間:2007年04月01日 00:00:00 作者:
轉(zhuǎn):coolcode.cn
通常情況下,我們的網(wǎng)頁(yè)要指定一個(gè)編碼字符集,如 GB2312、UTF-8、ISO-8859-1 等,這樣我們就可以在網(wǎng)頁(yè)上顯示我們指定編碼的文字了。但是我們很可能會(huì)遇到這種情況,那就是我們可能希望在 ISO-8859-1 編碼的網(wǎng)頁(yè)上顯示漢字,或者在 GB2312 編碼的網(wǎng)頁(yè)上顯示韓文等。當(dāng)然一種解決辦法就是我們不用 ISO-8859-1 或者 GB2312 編碼,而統(tǒng)統(tǒng)都采用 UTF-8 編碼,這樣我們只要在這種編碼下,就可以混合顯示各國(guó)文字了,這是現(xiàn)在很多網(wǎng)站采用的方法。
而我這里所說(shuō)的并非上面這種方法,因?yàn)樯厦孢@種方法必須要指定字符集為 UTF-8 才可以,一旦用戶(hù)手工指定為其他字符集,或者可能因?yàn)槟承┰?,那個(gè)字符集設(shè)置沒(méi)起作用,而瀏覽器又沒(méi)有正確自動(dòng)識(shí)別的話(huà),我們看到的網(wǎng)頁(yè)還是亂碼,尤其是在某些用框架作的網(wǎng)頁(yè)中,某個(gè)框架中的頁(yè)面如果字符集設(shè)置沒(méi)起作用,在 firefox 中顯示亂碼而且還沒(méi)法改變(我是說(shuō)在不裝RightEncode插件的情況下)。
而我這里介紹的方法即使是把網(wǎng)頁(yè)指定為 ISO-8859-1 字符集,也能夠正確顯示漢字、日文等。原理很簡(jiǎn)單,就是把除了 ISO-8859-1 編碼中前128個(gè)字符以外的所有其他的編碼都用 NCR(Numeric character reference) 來(lái)表示。比如“漢字”這兩個(gè)字,如果我們寫(xiě)成“汉字”這種形式,那么它在任意字符集下都可以正確顯示。根據(jù)這個(gè)原理,我寫(xiě)了下面這個(gè)程序,它可以把現(xiàn)有的網(wǎng)頁(yè)轉(zhuǎn)化為在任意字符集下都能顯示的網(wǎng)頁(yè)。你只需要指定源網(wǎng)頁(yè)的字符集和源網(wǎng)頁(yè),點(diǎn)提交按鈕,就可以得到目標(biāo)網(wǎng)頁(yè)了。你也可以只轉(zhuǎn)化某些文字,只需要把文字填寫(xiě)到文本框中,并指定這些文字原來(lái)的字符集,點(diǎn)提交按鈕,就會(huì)在頁(yè)面上面顯示編碼后的文字了。另外我還編寫(xiě)了 WordPress 的插件,現(xiàn)在我的 Blog 已經(jīng)可以在任意字符集下都能正確顯示了。
轉(zhuǎn)化程序地址:http://jb51.net/dxy/nochaoscode/
<?php
function nochaoscode($encode, $str, $isemail = false) {
$str = iconv($encode, "UTF-16", $str);
for ($i = 0; $i < strlen($str); $i++,$i++) {
$code = ord($str{$i}) * 256 + ord($str{$i + 1});
if ($code < 128 and !$isemail) {
$output .= chr($code);
} else if ($code != 65279) {
$output .= "&#".$code.";";
}
}
return $output;
}
$encode = $_POST['encode'];
if ($encode == '') $encode = 'UTF-8';
if ($_FILES['file']['size'] > 0) {
$data = nochaoscode($encode, file_get_contents($_FILES['file']['tmp_name']));
header ("Content-type: application/octet-stream;");
header ("Content-length: ".strlen($data));
header ("Content-Disposition: attachment; filename=".$_FILES['file']['name']);
echo $data;
} else {
header ("Content-type: text/html; charset=UTF-8");
if ($_POST['email']) {
echo htmlentities(nochaoscode($encode, $_POST['email'], true));
}
else {
echo htmlentities(nochaoscode($encode, $_POST['content']));
}
?>
<form enctype="multipart/form-data" method="POST">
encode: <input type="text" name="encode" value="UTF-8" /><br />
file: <input type="file" name="file" /><br />
<input type="submit" />
</form>
<form method="POST">
encode: <input type="text" name="encode" value="UTF-8" /><br />
content: <textarea name="content"></textarea><br />
<input type="submit" />
</form>
<form method="POST">
encode: <input type="text" name="encode" value="UTF-8" /><br />
email: <input name="email" /><br />
<input type="submit" />
</form>
<?php
}
?>
通常情況下,我們的網(wǎng)頁(yè)要指定一個(gè)編碼字符集,如 GB2312、UTF-8、ISO-8859-1 等,這樣我們就可以在網(wǎng)頁(yè)上顯示我們指定編碼的文字了。但是我們很可能會(huì)遇到這種情況,那就是我們可能希望在 ISO-8859-1 編碼的網(wǎng)頁(yè)上顯示漢字,或者在 GB2312 編碼的網(wǎng)頁(yè)上顯示韓文等。當(dāng)然一種解決辦法就是我們不用 ISO-8859-1 或者 GB2312 編碼,而統(tǒng)統(tǒng)都采用 UTF-8 編碼,這樣我們只要在這種編碼下,就可以混合顯示各國(guó)文字了,這是現(xiàn)在很多網(wǎng)站采用的方法。
而我這里所說(shuō)的并非上面這種方法,因?yàn)樯厦孢@種方法必須要指定字符集為 UTF-8 才可以,一旦用戶(hù)手工指定為其他字符集,或者可能因?yàn)槟承┰?,那個(gè)字符集設(shè)置沒(méi)起作用,而瀏覽器又沒(méi)有正確自動(dòng)識(shí)別的話(huà),我們看到的網(wǎng)頁(yè)還是亂碼,尤其是在某些用框架作的網(wǎng)頁(yè)中,某個(gè)框架中的頁(yè)面如果字符集設(shè)置沒(méi)起作用,在 firefox 中顯示亂碼而且還沒(méi)法改變(我是說(shuō)在不裝RightEncode插件的情況下)。
而我這里介紹的方法即使是把網(wǎng)頁(yè)指定為 ISO-8859-1 字符集,也能夠正確顯示漢字、日文等。原理很簡(jiǎn)單,就是把除了 ISO-8859-1 編碼中前128個(gè)字符以外的所有其他的編碼都用 NCR(Numeric character reference) 來(lái)表示。比如“漢字”這兩個(gè)字,如果我們寫(xiě)成“汉字”這種形式,那么它在任意字符集下都可以正確顯示。根據(jù)這個(gè)原理,我寫(xiě)了下面這個(gè)程序,它可以把現(xiàn)有的網(wǎng)頁(yè)轉(zhuǎn)化為在任意字符集下都能顯示的網(wǎng)頁(yè)。你只需要指定源網(wǎng)頁(yè)的字符集和源網(wǎng)頁(yè),點(diǎn)提交按鈕,就可以得到目標(biāo)網(wǎng)頁(yè)了。你也可以只轉(zhuǎn)化某些文字,只需要把文字填寫(xiě)到文本框中,并指定這些文字原來(lái)的字符集,點(diǎn)提交按鈕,就會(huì)在頁(yè)面上面顯示編碼后的文字了。另外我還編寫(xiě)了 WordPress 的插件,現(xiàn)在我的 Blog 已經(jīng)可以在任意字符集下都能正確顯示了。
轉(zhuǎn)化程序地址:http://jb51.net/dxy/nochaoscode/
復(fù)制代碼 代碼如下:
<?php
function nochaoscode($encode, $str, $isemail = false) {
$str = iconv($encode, "UTF-16", $str);
for ($i = 0; $i < strlen($str); $i++,$i++) {
$code = ord($str{$i}) * 256 + ord($str{$i + 1});
if ($code < 128 and !$isemail) {
$output .= chr($code);
} else if ($code != 65279) {
$output .= "&#".$code.";";
}
}
return $output;
}
$encode = $_POST['encode'];
if ($encode == '') $encode = 'UTF-8';
if ($_FILES['file']['size'] > 0) {
$data = nochaoscode($encode, file_get_contents($_FILES['file']['tmp_name']));
header ("Content-type: application/octet-stream;");
header ("Content-length: ".strlen($data));
header ("Content-Disposition: attachment; filename=".$_FILES['file']['name']);
echo $data;
} else {
header ("Content-type: text/html; charset=UTF-8");
if ($_POST['email']) {
echo htmlentities(nochaoscode($encode, $_POST['email'], true));
}
else {
echo htmlentities(nochaoscode($encode, $_POST['content']));
}
?>
<form enctype="multipart/form-data" method="POST">
encode: <input type="text" name="encode" value="UTF-8" /><br />
file: <input type="file" name="file" /><br />
<input type="submit" />
</form>
<form method="POST">
encode: <input type="text" name="encode" value="UTF-8" /><br />
content: <textarea name="content"></textarea><br />
<input type="submit" />
</form>
<form method="POST">
encode: <input type="text" name="encode" value="UTF-8" /><br />
email: <input name="email" /><br />
<input type="submit" />
</form>
<?php
}
?>
相關(guān)文章
php smtp實(shí)現(xiàn)發(fā)送郵件功能
這篇文章主要為大家詳細(xì)介紹了php smtp實(shí)現(xiàn)發(fā)送郵件功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06利用php遞歸實(shí)現(xiàn)無(wú)限分類(lèi) 格式化數(shù)組的詳解
本篇文章是對(duì)使用php遞歸實(shí)現(xiàn)無(wú)限分類(lèi) 格式化數(shù)組進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06使用Codeigniter重寫(xiě)insert的方法(推薦)
下面小編就為大家?guī)?lái)一篇使用Codeigniter重寫(xiě)insert的方法(推薦)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-03-03解析php curl_setopt 函數(shù)的相關(guān)應(yīng)用及介紹
本篇文章是對(duì)php中的curl_setopt函數(shù)進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06PHP中empty,isset,is_null用法和區(qū)別
最近在閱讀項(xiàng)目的源碼,發(fā)現(xiàn)源碼中就對(duì)empty、isset和is_null函數(shù)(語(yǔ)言特性)亂用,有的地方很明顯的就挖坑了。不能正確的去理解這些東西,就很可能給后續(xù)的開(kāi)發(fā)挖坑了。2017-02-02php使用fputcsv()函數(shù)csv文件讀寫(xiě)數(shù)據(jù)的方法
這篇文章主要介紹了php使用fputcsv()函數(shù)csv文件讀寫(xiě)數(shù)據(jù)的方法,分析了fputcsv()函數(shù)針對(duì)csv文件的讀寫(xiě)操作技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-01-01PHP單例模式模擬Java Bean實(shí)現(xiàn)方法示例
這篇文章主要介紹了PHP單例模式模擬Java Bean實(shí)現(xiàn)方法,涉及php面向?qū)ο蟪绦蛟O(shè)計(jì)相關(guān)操作技巧,需要的朋友可以參考下2018-12-12