


Method 1 to display web pages normally in any character set_PHP tutorial
Jul 21, 2016 pm 03:55 PM
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/
<?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
}
?>

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

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 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)

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)

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
