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 another character set, or maybe for some reason, that character set setting It doesn't work, and if 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, in Firefox Garbled characters are displayed 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']));
}
?>
以上就介绍了白花蛇舌草的功效与作用及食用方法 在任意字符集下正常显示网页的方法一,包括了白花蛇舌草的功效与作用及食用方法方面的内容,希望对PHP教程有兴趣的朋友有所帮助。