Encode.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. namespace stringEncode;
  3. class Encode {
  4. /**
  5. * The encoding that the string is currently in.
  6. *
  7. * @var string
  8. */
  9. protected $from;
  10. /**
  11. * The encoding that we would like the string to be in.
  12. *
  13. * @var string
  14. */
  15. protected $to;
  16. /**
  17. * Sets the default charsets for thie package.
  18. */
  19. public function __construct()
  20. {
  21. // default from encoding
  22. $this->from = 'CP1252';
  23. // default to encoding
  24. $this->to = 'UTF-8';
  25. }
  26. /**
  27. * Sets the charset that we will be converting to.
  28. *
  29. * @param string $charset
  30. * @chainable
  31. */
  32. public function to($charset)
  33. {
  34. $this->to = strtoupper($charset);
  35. return $this;
  36. }
  37. /**
  38. * Sets the charset that we will be converting from.
  39. *
  40. * @param string $charset
  41. * @chainable
  42. */
  43. public function from($charset)
  44. {
  45. $this->from = strtoupper($charset);
  46. }
  47. /**
  48. * Returns the to and from charset that we will be using.
  49. *
  50. * @return array
  51. */
  52. public function charset()
  53. {
  54. return [
  55. 'from' => $this->from,
  56. 'to' => $this->to,
  57. ];
  58. }
  59. /**
  60. * Attempts to detect the encoding of the given string from the encodingList.
  61. *
  62. * @param string $str
  63. * @param array $encodingList
  64. * @return bool
  65. */
  66. public function detect($str, $encodingList = ['UTF-8', 'CP1252'])
  67. {
  68. $charset = mb_detect_encoding($str, $encodingList);
  69. if ($charset === false)
  70. {
  71. // could not detect charset
  72. return false;
  73. }
  74. $this->from = $charset;
  75. return true;
  76. }
  77. /**
  78. * Attempts to convert the string to the proper charset.
  79. *
  80. * @return string
  81. */
  82. public function convert($str)
  83. {
  84. if ($this->from != $this->to)
  85. {
  86. $str = iconv($this->from, $this->to, $str);
  87. }
  88. if ($str === false)
  89. {
  90. // the convertion was a failure
  91. throw new Exception('The convertion from "'.$this->from.'" to "'.$this->to.'" was a failure.');
  92. }
  93. // deal with BOM issue for utf-8 text
  94. if ($this->to == 'UTF-8')
  95. {
  96. if (substr($str, 0, 3) == "\xef\xbb\xbf")
  97. {
  98. $str = substr($str, 3);
  99. }
  100. if (substr($str, -3, 3) == "\xef\xbb\xbf")
  101. {
  102. $str = substr($str, 0, -3);
  103. }
  104. }
  105. return $str;
  106. }
  107. }