CharsetHandler.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. /**
  3. * The CharsetHandler class converts between various charsets
  4. *
  5. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  6. * @copyright (c) 2003 amfphp.org
  7. * @package flashservices
  8. * @subpackage io
  9. * @version $Id: CharsetHandler.php,v 1.5 2005/07/05 07:40:53 pmineault Exp $
  10. */
  11. class CharsetHandler
  12. {
  13. function CharsetHandler($mode)
  14. {
  15. $this->_method = CharsetHandler::getMethod();
  16. $this->_phpCharset = CharsetHandler::getPhpCharset();
  17. $this->_sqlCharset = CharsetHandler::getSqlCharset();
  18. $this->_mode = $mode;
  19. $newset = "";
  20. if($this->_mode == "flashtophp")
  21. {
  22. $this->_fromCharset = "utf-8";
  23. $this->_toCharset = $this->_phpCharset;
  24. $newset = $this->_phpCharset;
  25. }
  26. else if($this->_mode == "phptoflash")
  27. {
  28. $this->_fromCharset = $this->_phpCharset;
  29. $this->_toCharset = "utf-8";
  30. $newset = $this->_phpCharset;
  31. }
  32. else if($this->_mode == "sqltophp")
  33. {
  34. $this->_fromCharset = $this->_sqlCharset;
  35. $this->_toCharset = $this->_phpCharset;
  36. $newset = $this->_sqlCharset;
  37. }
  38. else if($this->_mode == "sqltoflash")
  39. {
  40. $this->_fromCharset = $this->_sqlCharset;
  41. $this->_toCharset = "utf-8";
  42. $newset = $this->_sqlCharset;
  43. }
  44. //Don't try to reencode charsets for nothing
  45. if($this->_fromCharset == $this->_toCharset)
  46. {
  47. $this->_method = "none";
  48. }
  49. }
  50. function transliterate($string)
  51. {
  52. switch($this->_method)
  53. {
  54. case "none" :
  55. return $string;
  56. break;
  57. case "iconv":
  58. return iconv($this->_fromCharset,$this->_toCharset, $string);
  59. break;
  60. case "utf8_decode":
  61. return ($this->_mode == "flashtophp" ? utf8_decode($string) : utf8_encode($string));
  62. break;
  63. case "mbstring":
  64. return mb_convert_encoding($string, $this->_toCharset, $this->_fromCharset);
  65. break;
  66. case "recode":
  67. return recode_string($this->_fromCharset . ".." . $this->_toCharset, $string);
  68. break;
  69. default:
  70. return $string;
  71. break;
  72. }
  73. }
  74. /**
  75. * Sets the charset handling method
  76. *
  77. * @param string $location One of "none", "iconv", "mbstring", "recode"
  78. */
  79. function getMethod($val=NULL)
  80. {
  81. static $method = 0;
  82. if($val != NULL)
  83. {
  84. if($val == 'utf8_encode')
  85. {
  86. $val = 'utf8_decode';
  87. }
  88. $method = $val;
  89. }
  90. return $method;
  91. }
  92. function setMethod($val=0){
  93. return CharsetHandler::getMethod($val);
  94. }
  95. function getPhpCharset($val=NULL)
  96. {
  97. static $phpCharset = 0;
  98. if($val != NULL)
  99. {
  100. $phpCharset = strtolower($val);
  101. }
  102. return $phpCharset;
  103. }
  104. function setPhpCharset($val=0){
  105. return CharsetHandler::getPhpCharset($val);
  106. }
  107. function getSqlCharset($val=NULL)
  108. {
  109. static $sqlCharset = 0;
  110. if($val != NULL)
  111. {
  112. $sqlCharset = strtolower($val);
  113. }
  114. return $sqlCharset;
  115. }
  116. function setSqlCharset($val=0){
  117. return CharsetHandler::getSqlCharset($val);
  118. }
  119. }
  120. ?>