How to display a webpage normally in any character set _php tutorial

Source: Internet
Author: User
Ext.: coolcode.cn
Typically, our web page specifies a coded character set, such as GB2312, UTF-8, iso-8859-1, and so on, so that we can display the text we specify on the page. However, we are likely to encounter this situation, that is, we may want to display Chinese characters on iso-8859-1 encoded pages, or to display Korean on GB2312 encoded pages. Of course, a solution is that we do not iso-8859-1 or GB2312 coding, and all use UTF-8 encoding, so that we can only in this code, we will be able to mix the display of national text, which is now a lot of Web site adoption method.

And I'm not talking about this method, because the above method must specify the character set as UTF-8, once the user is manually designated as a different character set, or perhaps for some reason, the character set does not work, and the browser does not automatically recognize the correct, we see the page is garbled, Especially in some frames of web pages, a frame of the page if the character set does not work, in Firefox display garbled and can not be changed (I mean without the Rightencode plugin).

And the method I introduced here, even if the Web page is designated as iso-8859-1 character set, it can display Chinese characters, Japanese and so on correctly. The simple principle is that all other encodings except the first 128 characters in the ISO-8859-1 encoding are represented by NCR (Numeric character reference). For example, the word "kanji", if we write "Chinese character" this form, then it can be displayed correctly under any character set. Based on this principle, I wrote the following program, which can convert an existing Web page into a Web page that can be displayed in any character set. You only need to specify the character set of the source page and the source page, click the Submit button, you can get the target page. You can also only convert certain text, just fill in the text into the box, and specify the original character set of the text, click the Submit button, the page will display the encoded text. In addition, I also wrote a WordPress plugin, now my Blog can be in any character set will be displayed correctly.
Conversion program Address: http://jb51.net/dxy/nochaoscode/
Copy CodeThe code is as follows:
function Nochaoscode ($encode, $str, $isemail = False) {
$str = Iconv ($encode, "UTF-16", $str);
for ($i = 0; $i < strlen ($STR); $i + +, $i + +) {
$code = Ord ($str {$i}) * + + ord ($str {$i + 1});
if ($code <! $isemail) {
$output. = Chr ($code);
} else if ($code! = 65279) {
$output. = "the". $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 '));
}
?>



}
?>

http://www.bkjia.com/PHPjc/318233.html www.bkjia.com true http://www.bkjia.com/PHPjc/318233.html techarticle go: coolcode.cn Typically, our pages specify a coded character set, such as GB2312, UTF-8, iso-8859-1, and so on, so that we can display the text we specify on the page ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.