File tree Expand file tree Collapse file tree 2 files changed +38
-7
lines changed
Expand file tree Collapse file tree 2 files changed +38
-7
lines changed Original file line number Diff line number Diff line change 22
33namespace Embed \Http ;
44
5+ use Embed \Utils ;
56use Exception ;
67use DOMDocument ;
78use 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}
Original file line number Diff line number Diff 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}
You can’t perform that action at this time.
0 commit comments