Method 1 to display web pages normally in any character set_PHP tutorial

WBOY
Release: 2016-07-21 15:55:43
Original
764 people have browsed it

Transfer to: coolcode.cn
Normally, our webpage needs to specify an encoding character set, such as GB2312, UTF-8, ISO-8859-1, etc., so that we can display the text in our specified encoding on the webpage. . But we are likely to encounter this situation, that is, we may want to display Chinese characters on ISO-8859-1 encoded web pages, or display Korean characters on GB2312 encoded web pages, etc. Of course, one solution is that we don’t use ISO-8859-1 or GB2312 encoding, but use UTF-8 encoding. In this way, as long as we use this encoding, we can display a mixture of languages ​​from various countries. This is what many websites now use. method.

What I am talking about here is not the above method, because the above method must specify the character set as UTF-8. Once the user manually specifies other character sets, or maybe for some reasons , that character set setting does not work, and the browser does not automatically recognize it correctly, the web pages we see will still be garbled, especially in some web pages made with frames. If the character set setting of a page in a frame does not work, the web page we see will still be garbled. It works, but garbled characters are displayed in firefox and cannot be changed (I mean without installing the RightEncode plug-in).

The method I introduce here can correctly display Chinese characters, Japanese, etc. even if the web page is specified as the ISO-8859-1 character set. The principle is very simple, that is, all other encodings except the first 128 characters in the ISO-8859-1 encoding are represented by NCR (Numeric character reference). For example, if we write the two characters "Chinese characters" in the form of "Chinese characters", then they can be displayed correctly in any character set. Based on this principle, I wrote the following program, which can convert existing web pages into web pages that can be displayed in any character set. You only need to specify the character set and source web page of the source web page, click the submit button, and you will get the target web page. You can also convert only certain text. You only need to fill in the text in the text box, specify the original character set of these texts, click the submit button, and the encoded text will be displayed on the page. In addition, I also wrote a WordPress plug-in, and now my blog can be displayed correctly in any character set.
Conversion program address: http://jb51.net/dxy/nochaoscode/

Copy code The 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}) * 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']));
    }
?>

encode: 
file: 



encode: 
content: 



encode: 
email: 


}
?>

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/318233.htmlTechArticle转:coolcode.cn 通常情况下,我们的网页要指定一个编码字符集,如GB2312、UTF-8、ISO-8859-1等,这样我们就可以在网页上显示我们指定编码的文字...
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!