How to display Web pages normally under any character set

Source: Internet
Author: User
Tags character set header ord strlen
Page | show
  • Typically, our web page will 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. But we are likely to encounter the situation where we may want to display Chinese characters on iso-8859-1-encoded web pages, or to display Korean on GB2312-encoded web pages. Of course, a solution is that we do not use iso-8859-1 or GB2312 coding, and all using UTF-8 code, so that we can only be in this code, the mixed display of national characters, which is now a number of Web sites using the method.

    And what I'm saying here is not the above method, because the above method must specify the character set for UTF-8 only, once the user manually assigned to other character sets, or perhaps for some reason, that character set settings did not work, and the browser does not correctly automatically identify the words, we see the page or garbled, Especially in some frames of the Web page, a frame of the page if the character set settings do not work, in Firefox display garbled and can not be changed (I mean in the case of not installing Rightencode plug-ins).

    And the method I introduced here even if the Web page is designated as the Iso-8859-1 character set, can also correctly display Chinese characters, Japanese and so on. The simple principle is that all other encodings except the first 128 characters in the ISO-8859-1 code are represented by NCR (Numeric character reference). For example, the words "Chinese characters", if we write "Chinese" in this form, then it can be displayed correctly in any character set. Based on this principle, I wrote the following program, which transforms the existing Web page into a Web page that can be displayed in any character set. You only need to specify the source page of the character set and source page, click the Submit button, you can get the target page. You can also only convert some text, just fill in the text box, and specify the original character set of the text, point submit button, it will appear on the page encoded text. In addition, I also wrote WordPress plug-ins, now my Blog can be in any character set can be displayed correctly.

    Conversion program Address: http://test.coolcode.cn/nochaoscode/

    Download: nochaoscode.php
    <?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 

    ?>



  • 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.