- PHP Kanji transcoding
- Class qswhu{
- var $qswhData;
- function Qswhu ($filename = "qswhu.php") {
- $this->qswhdata=file ($filename);
- }
- function decode ($STR, $pattern =0) {
- $arr =array ("/&# (/w+);/iu", "/((%/W/W) +)/I", "/%u (/w{4,5})/iu");
- if (Is_integer ($pattern)) {
- if ($pattern >=count ($arr)) Die ("Invalid Function");
- $pattern = $arr [$pattern];
- }
- Return Preg_replace_callback ($pattern, Array ($this, "U2GB"), $STR);
- }//Programmer's Home bbs.it-home.org
- function U2GB ($arr) {
- /****** (Qiushuiwuhen 2002-8-15) ******/
- $ret = ""; $str = $arr [1];
- if (Preg_match_all ("/%/w{2}/", $str, $matches)) {
- for ($i =0; $i<>
- $CHR 1=hexdec (substr ($matches [0][$i],1);
- $arr =array ("F0", "E0", "C0", "0");
- For ($j =0, $j hexdec ($arr [$j])) break;
- $CHR =hexdec (substr ($matches [0][$i],1)-hexdec ($arr [$j]);
- while (+ + $j<>
- $str =dechex ($CHR);
- if (strlen ($STR) ==4) {
- $p =hexdec (substr ($str, 0,2)) -0x4d;
- $q =hexdec (substr ($STR, 2));
- $ret. =CHR (Hexdec (substr ($this->qswhdata[$p], $q, 2)));
- $ret. =CHR (Hexdec (substr ($this->qswhdata[$p], $q +2,2)));
- }else
- $ret. =CHR (Hexdec ($STR));
- }
- }
- else{
- if (Strtolower ($str [0]) = = "X")
- $str =substr ($STR, 1);
- Else
- if (strlen ($STR)!=4) $str =dechex ($STR);
- if (strlen ($STR) ==4) {
- $p =hexdec (substr ($str, 0,2)) -0x4d;
- $q =hexdec (substr ($STR, 2));
- $ret. =CHR (Hexdec (substr ($this->qswhdata[$p], $q, 2)));
- $ret. =CHR (Hexdec (substr ($this->qswhdata[$p], $q +2,2)));
- }else
- $ret. =CHR (Hexdec ($STR));
- }
- return $ret;
- }
- }
Copy CodeUsage examples:
-
- $qswh =new qswhu ("qswhu.php");//If the file name is qswhu.php, you can save the parameter
- echo without parameters (default filter is: &amp;#[num ];): "; &lt;li&gt;echo"/n ". $qswh-&gt;decode (" &amp; #x4E2D;&amp; #x6587;&amp; #x41;&amp; #x62;&amp; #x63; "); &lt;li&gt;echo "/n". $qswh-&gt;decode ("&amp; #20013;&amp; #25991;&amp; #65;&amp; #98;&amp; #99;"); &lt;li&gt;echo "/n calls built-in filtering (UTF transcoding):". $qswh-&gt;decode ("%e4%b8%ad%e6%96%87%20!%22%23%24%25%26" () *%2b%2c%2f%3a%3b%3c %3d%3e%3f%40%5b%5d%5e%60%7b%7c%7d~%25abc ", 1); &lt;li&gt;echo"/n calls built-in filter unescape (%u[num]): ". $qswh-&gt;decode ("% U4e2d%u6587abc ", 2); &lt;li&gt;echo"/N Custom filter ([X+num]): ". $qswh-&gt;decode (" [x4e2d][x6587][x41][x62][x63] ","//[(/w+ )/]/"); &lt;/ol&gt;&lt;em onclick=" Copycode ($ (' code_hjp ')); " &gt; Copy Code &lt;/em&gt;&lt;p&gt; Effect as follows: Without parameters (default filter is: &amp;#[num];): Chinese ABC chinese ABC call built-in filter (UTF transcoding): Chinese! " #$%&amp; ' () *+,/:;&lt;=&gt;?@[]^ ' {|} ~%ABC call built-in filter unescape (%u[num]): Chinese ABC custom filter ([X+num]): Chinese abc&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;li &gt; &lt;i class= "Layui-icon" &gt;&amp; #xe63a;
|