Php Chinese character transcoding GBK to Big5 encoding conversion

Source: Internet
Author: User
Php Chinese character transcoding GBK to Big5 encoding conversion

  1. // Php Chinese character transcoding
  2. Class qswhBig5 {
  3. Var $ qswhData;
  4. Function qswhBig5 ($ filename = "qswhBig5.php "){
  5. $ This-> qswhData = file ($ filename );
  6. }
  7. Function qswhStr ($ gb, $ fail = "?? "){
  8. /***** (Qiushuiwuhen 2002-9-6 )******/
  9. $ Ret = "";
  10. For ($ I = 0; $ I If ($ p = ord (substr ($ gb, $ I, 1)> 127 ){
  11. $ Q = ord (substr ($ gb, ++ $ I, 1 ));
  12. $ Tmp = $ this-> qswhData [$ p-128];
  13. For ($ j = 0; $ j = $ Q) break;
  14. If ($ k = $ q) $ q = chr (hexdec (substr ($ tmp, $ j + 2, 2 ))). chr (hexdec (substr ($ tmp, $ j + 4, 2 )));
  15. Else if ($ fail = "") $ q = chr ($ p). chr ($ q); else $ q = $ fail;
  16. }
  17. Else
  18. $ Q = chr ($ p );
  19. $ Ret. = $ q;
  20. }
  21. Return $ ret;
  22. }
  23. Function qswhFile ($ filename, $ fail = "?? "){
  24. /***** (Qiushuiwuhen 2002-9-6 )******/
  25. If (! File_exists ($ filename. ". qswh") copy ($ filename, $ filename. ". qswh ");
  26. $ Fp = fopen ($ filename, "r + ");
  27. $ Tmp = fread ($ fp, filesize ($ filename ));
  28. Rewind ($ fp );
  29. Fwrite ($ fp, $ this-> qswhStr ($ tmp, $ fail ));
  30. Fclose ($ fp );
  31. }
  32. Function qswhDir ($ dirname, $ fail = "?? "){
  33. /***** (Qiushuiwuhen 2002-9-6 )******/
  34. $ D = dir ($ dirname );
  35. While ($ entry = $ d-> read ()){
  36. If ($ entry = "." | $ entry = "...") continue;
  37. $ Entry = $ dirname. "/". $ entry;
  38. If (is_dir ($ entry ))
  39. $ This-> qswhDir ($ entry, $ fail );
  40. Else {
  41. If (substr ($ entry,-5 )! = ". Qswh") $ this-> qswhFile ($ entry, $ fail );
  42. }
  43. }
  44. $ D-> close ();
  45. Return $ c;
  46. }
  47. }

Example:

  1. $ Words = "no hate in autumn ";

  2. $ Qswh = new qswhBig5 ("qswhBig5.php"); // qswhBig5.php, which saves parameters

  3. Echo ("

    Please select the encoding Big5 to view :". $ qswh-> qswhStr ($ words); <li> echo ("/n encode a directory :". $ qswh-> qswhDir ("test"); <li> echo ("/n encode a File :". $ qswh-> qswhFile ("index.htm ")); </p> <li> </ol> </p> <em onclick = "copycode ($ ('code _ L1e ')); "> </em> </p> <p> Note: The second parameter in qswhStr indicates the word that will be replaced if no corresponding Big5 word exists. if it is null, the word is retained. After formatting the file, remember to modify the content-type in <meta to Big. </P> </td> </tr> </table> </p> <p id = "comment_46681" class = "cm"> </p> <p id = "post_rate_p_46681">

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.