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