Home Backend Development PHP Tutorial Method 1 to display web pages normally in any character set_PHP tutorial

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

Jul 21, 2016 pm 03:55 PM
one Down exist character set case us designation method show normal of Web page want change

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:

<?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
}
?>

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/318233.htmlTechArticle转:coolcode.cn 通常情况下,我们的网页要指定一个编码字符集,如GB2312、UTF-8、ISO-8859-1等,这样我们就可以在网页上显示我们指定编码的文字...
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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to send web pages to desktop as shortcut in Edge browser? How to send web pages to desktop as shortcut in Edge browser? Mar 14, 2024 pm 05:22 PM

How to send web pages to desktop as shortcut in Edge browser?

How to write a novel in the Tomato Free Novel app. Share the tutorial on how to write a novel in Tomato Novel. How to write a novel in the Tomato Free Novel app. Share the tutorial on how to write a novel in Tomato Novel. Mar 28, 2024 pm 12:50 PM

How to write a novel in the Tomato Free Novel app. Share the tutorial on how to write a novel in Tomato Novel.

How to enter bios on Colorful motherboard? Teach you two methods How to enter bios on Colorful motherboard? Teach you two methods Mar 13, 2024 pm 06:01 PM

How to enter bios on Colorful motherboard? Teach you two methods

How to recover deleted contacts on WeChat (simple tutorial tells you how to recover deleted contacts) How to recover deleted contacts on WeChat (simple tutorial tells you how to recover deleted contacts) May 01, 2024 pm 12:01 PM

How to recover deleted contacts on WeChat (simple tutorial tells you how to recover deleted contacts)

Quickly master: How to open two WeChat accounts on Huawei mobile phones revealed! Quickly master: How to open two WeChat accounts on Huawei mobile phones revealed! Mar 23, 2024 am 10:42 AM

Quickly master: How to open two WeChat accounts on Huawei mobile phones revealed!

The secret of hatching mobile dragon eggs is revealed (step by step to teach you how to successfully hatch mobile dragon eggs) The secret of hatching mobile dragon eggs is revealed (step by step to teach you how to successfully hatch mobile dragon eggs) May 04, 2024 pm 06:01 PM

The secret of hatching mobile dragon eggs is revealed (step by step to teach you how to successfully hatch mobile dragon eggs)

How to set font size on mobile phone (easily adjust font size on mobile phone) How to set font size on mobile phone (easily adjust font size on mobile phone) May 07, 2024 pm 03:34 PM

How to set font size on mobile phone (easily adjust font size on mobile phone)

What should I do if the images on the webpage cannot be loaded? 6 solutions What should I do if the images on the webpage cannot be loaded? 6 solutions Mar 15, 2024 am 10:30 AM

What should I do if the images on the webpage cannot be loaded? 6 solutions

See all articles