convEncoding.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. function toUtf8($ar)
  3. {
  4. $c = '';
  5. foreach($ar as $val)
  6. {
  7. $val = intval(substr($val,2),16);
  8. if($val < 0x7F)
  9. { // 0000-007F
  10. $c .= chr($val);
  11. }
  12. elseif($val < 0x800)
  13. { // 0080-0800
  14. $c .= chr(0xC0 | ($val / 64));
  15. $c .= chr(0x80 | ($val % 64));
  16. }
  17. else
  18. { // 0800-FFFF
  19. $c .= chr(0xE0 | (($val / 64) / 64));
  20. $c .= chr(0x80 | (($val / 64) % 64));
  21. $c .= chr(0x80 | ($val % 64));
  22. }
  23. }
  24. return $c;
  25. }
  26. function uniDecode($str, $charcode="UTF-8")
  27. {
  28. $text = preg_replace_callback("/%u[0-9A-Za-z]{4}/", 'toUtf8', $str);
  29. return mb_convert_encoding($text, $charcode, 'utf-8');
  30. }
  31. function cp($m)
  32. {
  33. return iconv('UTF-8', 'GBK', uniDecode('%u' . dechex($m[1])));
  34. }
  35. function c($str)
  36. {
  37. return preg_replace_callback('/&#([0-9]+);/', 'cp', $str);
  38. }
  39. if (isset($_GET['fileName']))
  40. {
  41. $fileName = $_GET['fileName'];
  42. }
  43. else if(isset($argv[1]))
  44. {
  45. $fileName = $argv[1];
  46. }
  47. else
  48. {
  49. echo "Usage: In the command line: php scriptName fileName or on the web http://foobar/scriptName.php?fileName=foobar";
  50. }
  51. if (isset($_GET['output']))
  52. {
  53. $output = $_GET['output'];
  54. }
  55. elseif(isset($argv[2]))
  56. {
  57. $output = $argv[2];
  58. }
  59. else
  60. {
  61. $p = pathinfo($fileName);
  62. $output = $p['basename'];
  63. }
  64. $file = file_get_contents($fileName);
  65. $file = c($file);
  66. file_put_contents($output, $file);