Ext: coolcode.cn
A few days ago wrote an arbitrary character set in the normal display of the Web page, which is very simple, is the first 128 characters other than the character set used NCR to express, but specifically how to transform I did not introduce, because at that time I feel too simple. But then found that someone asked the question, here is a detailed description.
The first step is to convert the string of the source character set to the UTF-16 character set, which is done because each character in the UTF-16 character set is two bytes long, which is easy to deal with, and complex to do directly on the source character set. The source character set can be obtained from the META tags in the original Web page. Can also be specified separately, my program is to allow users to specify the source character set in the form, because I can not guarantee that the user submitted files must be HTML files (other files are also possible, such as this WordPress Chinese package source file is a PO file , the contents of it can also be handled in this way), and even the HTML file does not necessarily have a meta tag for the specified character set, so it is safe to specify the character set by the form alone. You may find it complex to convert one character set to another, sure, it's really troublesome to do it yourself, but it's easy to do it in PHP, because it already contains such a function, and you can easily convert between the various character sets through the Iconv function. If you have not installed Iconv extensions on your machine, you can also use the mb_convert_encoding function, and if multibyte String extension is not installed, then there is no way, because you have to implement so many of the encoding of the conversion is basically impossible, Unless you're a top bull! The use of Iconv is recommended because it is efficient and supports more character sets.
After the above step, the string is then processed in every two bytes. These two bytes directly into the number is the & #xxxxx; XXXXX, if this number is less than 128 to use this character directly (note that this becomes single-byte), otherwise use & #xxxxx; The form. One thing to note here is that when this number is 65279 (16 binary 0xFEFF), ignore it, because this is the transmission control character in the Unicode encoding, and our current string is only the first 128 characters in the ISO-8859-1 encoding, So we don't need it anymore.
Well, the basic idea is this, here is the implementation of the program:
Download: nochaoscode.php
Copy Code code as follows:
<?php
function Nochaoscode ($encode, $str) {
$str = Iconv ($encode, "Utf-16be", $str);
for ($i = 0; $i < strlen ($STR); $i + +, $i + +) {
$code = Ord ($str {$i}) * 256 + ord ($str {$i + 1});
if ($code < 128) {
$output. = Chr ($code);
else if ($code!= 65279) {
$output. = "&#". $code. ";";
}
}
return $output;
}
?>
In the parameter of the function, $encode is the source character set, $str is the string that needs to be transformed. The return result is the conversion of the string later.
Add: Today Legend tells me a simpler way is to use the mb_convert_encoding function directly. Because mb_convert_encoding supports a coded format called Html-entities, which is NCR code. It's easier to use it.