Skip to content

Commit e20ea31

Browse files
committed
added support for windows-1250 #180
1 parent 65862e3 commit e20ea31

File tree

2 files changed

+38
-7
lines changed

2 files changed

+38
-7
lines changed

src/Http/Response.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Embed\Http;
44

5+
use Embed\Utils;
56
use Exception;
67
use DOMDocument;
78
use SimpleXMLElement;
@@ -131,13 +132,7 @@ private function setContent($content)
131132
list($mime, $charset) = array_map('trim', explode(';', $this->contentType));
132133

133134
$this->contentType = $mime;
134-
135-
//Convert the content to UTF-8
136-
$charset = substr(strtoupper(strstr($charset, '=')), 1);
137-
138-
if (!empty($charset) && !empty($content) && ($charset !== 'UTF-8')) {
139-
$this->content = mb_convert_encoding($content, 'UTF-8', $charset);
140-
}
135+
$this->content = Utils::toUtf8($content, substr(strstr($charset, '='), 1));
141136
}
142137
}
143138
}

src/Utils.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,4 +187,40 @@ private static function element($name, array $attrs)
187187

188188
return "$str>";
189189
}
190+
191+
/**
192+
* Convert a string to utf-8.
193+
*
194+
* @param string $content The content to convert
195+
* @param string $charset The original charset
196+
*
197+
* @return string
198+
*/
199+
public static function toUtf8($content, $charset)
200+
{
201+
$charset = strtoupper($charset);
202+
203+
if (empty($charset) || $charset === 'UTF-8') {
204+
return $content;
205+
}
206+
207+
if (function_exists('iconv_set_encoding')) {
208+
$prev = iconv_get_encoding('all');
209+
iconv_set_encoding('php.input_encoding', $charset);
210+
iconv_set_encoding('php.output_encoding', 'UTF-8');
211+
ob_start('ob_iconv_handler');
212+
echo $content;
213+
$content = ob_get_clean();
214+
iconv_set_encoding('php.input_encoding', $prev['input_encoding']);
215+
iconv_set_encoding('php.output_encoding', $prev['output_encoding']);
216+
217+
return $content;
218+
}
219+
220+
if (function_exists('mb_convert_encoding')) {
221+
return mb_convert_encoding($content, 'UTF-8', $charset);
222+
}
223+
224+
return $content;
225+
}
190226
}

0 commit comments

Comments
 (0)