PHP does not use the Iconv library for gb2312 and UTF-8 encoding conversion functions

Source: Internet
Author: User
Tags comparison table utf 8
  1. Use of the comparison table

  2. $filename = "Gb2utf8.txt";
  3. $fp = fopen ($filename, "R");
  4. while (! feof ($fp)) {
  5. List ($GB, $utf 8) = Fgetcsv ($fp, 10);
  6. $charset [$GB] = $utf 8;
  7. }
  8. Fclose ($FP);
  9. Read the table above to the array alternate

  10. /** gb2312 to Utf-8 **/

  11. function Gb2utf8 ($text, & $charset) {
  12. Extract the elements in the text, the kanji as an element, and a continuous non-Chinese character as an element
  13. Preg_match_all ("/(?: [\x80-\xff].) | [\x01-\x7f]+/], $text, $tmp);
  14. $tmp = $tmp [0];
  15. Separating out Chinese characters
  16. $ar = Array_intersect ($tmp, Array_keys ($charset));
  17. Replace Chinese character coding
  18. foreach ($ar as $k = $v)
  19. $tmp [$k] = $charset [$v];
  20. Returns the string after the Exchange code
  21. return join (' ', $tmp);
  22. }

  23. /** Utf-8 to gb2312 **/

  24. function UTF82GB ($text, & $charset) {
  25. $p = "/[xf0-xf7][x80-xbf]{3}| [Xe0-xef] [X80-XBF] {2}| [XC2-XDF] [x80-xbf]| [x01-x7f]+/];
  26. Preg_match_all ($p, $text, $r);
  27. $utf 8 = Array_flip ($charset);
  28. foreach ($r [0] as $k = $v)
  29. if (Isset ($utf 8[$v]))
  30. $r [0][$k] = $utf 8[$v];
  31. return join ("', $r [0]);
  32. }

  33. Test

  34. $s = Gb2utf8 (' This is the test of the comparison table ', $charset);
  35. Echo utf82gb ($s, $charset);
  36. ?>

Copy Code
  • Related Article

    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.