3v4l.org

run code in 300+ PHP versions simultaneously
<?php ini_set("error_reporting", -1 & ~E_DEPRECATED); set_error_handler(function($s, $m, $f, $l, $c = null) { print "PHP Warning: $m in " . basename($f) . " on line $l"; }, E_WARNING); $xml = base64_decode("PGRpdj48cD7M4OzgIOz76+Ag8ODs8y48L3A+PC9kaXY+"); $options = LIBXML_HTML_NOIMPLIED | LIBXML_NOENT | LIBXML_NONET | LIBXML_NOWARNING | LIBXML_NOXMLDECL | LIBXML_HTML_NODEFDTD ; print "<pre>"; foreach([ 'default_charset', 'input_encoding', 'internal_encoding', 'output_encoding', 'iconv.input_encoding', 'iconv.internal_encoding', 'iconv.output_encoding', 'mbstring.http_input', 'mbstring.http_output', 'mbstring.internal_encoding', ] as $set) { ini_set($set, "UTF-8"); printf("%-12s: %s\n", $set, ini_get($set)); } print "</pre>"; ob_start(function($str, $phase) { return iconv("Windows-1251", "UTF-8", $str); }); print "--- CP1251 ---\n$xml\n"; print "Direct:"; $doc = new DOMDocument; $doc->loadHTML("$xml", $options); $doc->encoding = "Windows-1251"; print $doc->saveXML(null, LIBXML_NOENT); print "With encoding:"; $doc = new DOMDocument("1.0", "Windows-1251"); $doc->loadHTML("$xml", $options); $doc->encoding = "Windows-1251"; print $doc->saveXML(null, LIBXML_NOENT); print "With inline encoding:"; $doc = new DOMDocument; $doc->loadHTML("<?xml version=\"1.0\" encoding=\"Windows-1251\" standalone=\"yes\"?>$xml", $options); $doc->encoding = "Windows-1251"; print $doc->saveXML(null, LIBXML_NOENT); print "With both:"; $doc = new DOMDocument("1.0", "Windows-1251"); $doc->loadHTML("<?xml version=\"1.0\" encoding=\"Windows-1251\" standalone=\"yes\"?>$xml", $options); $doc->encoding = "Windows-1251"; print $doc->saveXML(null, LIBXML_NOENT); ob_end_flush(); $xml = iconv("Windows-1251", "UTF-8", $xml); print "--- UTF-8 ---\n$xml\n"; print "Direct:\n"; $doc = new DOMDocument; $doc->loadHTML("$xml", $options); $doc->encoding = "UTF-8"; print $doc->saveXML(null, LIBXML_NOENT); print "With encoding:\n"; $doc = new DOMDocument("1.0", "UTF-8"); $doc->loadHTML("$xml", $options); $doc->encoding = "UTF-8"; print $doc->saveXML(null, LIBXML_NOENT); print "With inline encoding:"; $doc = new DOMDocument; $doc->loadHTML("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>$xml", $options); $doc->encoding = "UTF-8"; print $doc->saveXML(null, LIBXML_NOENT); print "With both:"; $doc = new DOMDocument("1.0", "UTF-8"); $doc->loadHTML("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>$xml", $options); $doc->encoding = "UTF-8"; print $doc->saveXML(null, LIBXML_NOENT);
Output for 5.6.0 - 5.6.40, 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.21, 7.4.0 - 7.4.10, 8.0.0alpha1 - beta3
<pre>default_charset: UTF-8 input_encoding: UTF-8 internal_encoding: UTF-8 output_encoding: UTF-8 iconv.input_encoding: UTF-8 iconv.internal_encoding: UTF-8 iconv.output_encoding: UTF-8 mbstring.http_input: UTF-8 mbstring.http_output: UTF-8 mbstring.internal_encoding: UTF-8 </pre>--- CP1251 --- <div><p>Мама мыла раму.</p></div> Direct:<?xml version="1.0" encoding="Windows-1251" standalone="yes"?> <div><p>&#204;&#224;&#236;&#224; &#236;&#251;&#235;&#224; &#240;&#224;&#236;&#243;.</p></div> With encoding:<?xml version="1.0" encoding="Windows-1251" standalone="yes"?> <div><p>&#204;&#224;&#236;&#224; &#236;&#251;&#235;&#224; &#240;&#224;&#236;&#243;.</p></div> With inline encoding:PHP Warning: DOMDocument::loadHTML(): Input is not proper UTF-8, indicate encoding ! in Entity, line: 1 in HL5It on line 56PHP Warning: DOMDocument::saveXML(): output conversion failed due to conv error, bytes 0xCC 0xC3 0xA0 0xC3 in HL5It on line 58<?xml version="1.0" encoding="Windows-1251" standalone="yes"?> <?xml version="1.0" encoding="Windows-1251" standalone="yes"??> <div><p>With both:PHP Warning: DOMDocument::loadHTML(): encoder errorInput is not proper UTF-8, indicate encoding ! in Entity, line: 1 in HL5It on line 62PHP Warning: DOMDocument::saveXML(): output conversion failed due to conv error, bytes 0xCC 0xC3 0xA0 0xC3 in HL5It on line 64<?xml version="1.0" encoding="Windows-1251" standalone="yes"?> <?xml version="1.0" encoding="Windows-1251" standalone="yes"??> <div><p>--- UTF-8 --- <div><p>Мама мыла раму.</p></div> Direct: <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <div><p>Мама мыла раму.</p></div> With encoding: <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <div><p>Мама мыла раму.</p></div> With inline encoding:<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <?xml version="1.0" encoding="UTF-8" standalone="yes"??> <div><p>Мама мыла раму.</p></div> With both:<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <?xml version="1.0" encoding="UTF-8" standalone="yes"??> <div><p>Мама мыла раму.</p></div>