3v4l.org

run code in 300+ PHP versions simultaneously
<?php function output_delimiter() { echo "\n-----------------------------------------------------------------------------\n\n"; } /* * It decodes strings containig encoded words by following the steps below * 1) Remove MIME header line breaks. * 2) Decode encoded words by isolating pure encoded words and * decode them with imap_utf8 * * @param $str */ function imap_utf8_wrapper($str) { $str=preg_replace('/\r\n\s/', "", $str); return preg_replace_callback('/=\?[^?]+\?(?:Q|B)\?[^?]+\?=/', function($matches) { return imap_utf8($matches[0]); }, $str); } function imap_mime_header_decode_wrapper($str) { $arr=imap_mime_header_decode($str); $ret=""; foreach($arr as $elem) { if($elem->charset == 'default') $ret.=$elem->text; else $ret.=iconv($elem->charset, "UTF-8", $elem->text); } return $ret; } function decode_suite($str) { echo " iconv_mime_decode : ".iconv_mime_decode($str, ICONV_MIME_DECODE_CONTINUE_ON_ERROR, "UTF-8")."\n"; echo " mb_decode_mimeheader : ".mb_decode_mimeheader($str)."\n"; echo " imap_utf8_wrapper : ".imap_utf8_wrapper($str)."\n"; echo " imap_mime_header_decode : ".imap_mime_header_decode_wrapper($str)."\n"; } function mime_encoding_decoding_test($str, $encode_method, $func) { $str=$func($str); echo "After $encode_method : $str\n\n"; decode_suite($str); output_delimiter(); } function execute_test($str) { echo "\nBefore encoding : ".$str."\n"; output_delimiter(); $mime_test=function ($encode_method, $func) use ($str) { mime_encoding_decoding_test($str, $encode_method, $func); }; $prefs = array ("scheme" => "Q", "input-charset" => "utf-8", "output-charset" => "utf-8", "line-break-chars" => "\r\n"); $mime_test("iconv_mime_encode Q", function($str) use ($prefs) { return iconv_mime_encode('From', $str, $prefs); }); $prefs['scheme']="B"; $mime_test("iconv_mime_encode B", function($str) use ($prefs) { return iconv_mime_encode('From', $str, $prefs); }); $mime_test("mb_encode_mimeheader Q", function($str) { return mb_encode_mimeheader($str, "UTF-8", "Q"); }); $mime_test("mb_encode_mimeheader B", function($str) { return mb_encode_mimeheader($str, "UTF-8", "B"); }); } $str='"Réal Namé" <user@example.com>'; #$str='안녕 <user@example.com>'; execute_test($str); ?>
Output for 8.2.0 - 8.2.17, 8.3.0 - 8.3.4
Before encoding : "Réal Namé" <user@example.com> ----------------------------------------------------------------------------- After iconv_mime_encode Q : From: =?utf-8?Q?"R=C3=A9al=20Nam=C3=A9"=20<user@example.com>?= iconv_mime_decode : From: "Réal Namé" <user@example.com> mb_decode_mimeheader : From: "Réal Namé" <user@example.com> Fatal error: Uncaught Error: Call to undefined function imap_utf8() in /in/SYFeT:17 Stack trace: #0 [internal function]: {closure}(Array) #1 /in/SYFeT(16): preg_replace_callback('/=\\?[^?]+\\?(?:Q...', Object(Closure), 'From: =?utf-8?Q...') #2 /in/SYFeT(37): imap_utf8_wrapper('From: =?utf-8?Q...') #3 /in/SYFeT(44): decode_suite('From: =?utf-8?Q...') #4 /in/SYFeT(53): mime_encoding_decoding_test('From: =?utf-8?Q...', 'iconv_mime_enco...', Object(Closure)) #5 /in/SYFeT(60): {closure}('iconv_mime_enco...', Object(Closure)) #6 /in/SYFeT(76): execute_test('"R\xC3\xA9al Nam\xC3\xA9" <...') #7 {main} thrown in /in/SYFeT on line 17
Process exited with code 255.
Output for 7.1.23 - 7.1.33, 7.2.11 - 7.2.33, 7.3.0 - 7.3.31, 7.4.0 - 7.4.32, 8.0.0 - 8.0.12, 8.0.14 - 8.0.30, 8.1.0 - 8.1.27
Before encoding : "Réal Namé" <user@example.com> ----------------------------------------------------------------------------- After iconv_mime_encode Q : From: =?utf-8?Q?"R=C3=A9al=20Nam=C3=A9"=20<user@example.com>?= iconv_mime_decode : From: "Réal Namé" <user@example.com> mb_decode_mimeheader : From: "Réal Namé" <user@example.com> Fatal error: Uncaught Error: Call to undefined function imap_utf8() in /in/SYFeT:17 Stack trace: #0 [internal function]: {closure}(Array) #1 /in/SYFeT(17): preg_replace_callback('/=\\?[^?]+\\?(?:Q...', Object(Closure), 'From: =?utf-8?Q...') #2 /in/SYFeT(37): imap_utf8_wrapper('From: =?utf-8?Q...') #3 /in/SYFeT(44): decode_suite('From: =?utf-8?Q...') #4 /in/SYFeT(53): mime_encoding_decoding_test('From: =?utf-8?Q...', 'iconv_mime_enco...', Object(Closure)) #5 /in/SYFeT(61): {closure}('iconv_mime_enco...', Object(Closure)) #6 /in/SYFeT(76): execute_test('"R\xC3\xA9al Nam\xC3\xA9" <...') #7 {main} thrown in /in/SYFeT on line 17
Process exited with code 255.
Output for 7.3.32 - 7.3.33, 7.4.33, 8.0.13
Before encoding : "Réal Namé" <user@example.com> ----------------------------------------------------------------------------- Fatal error: Uncaught Error: Call to undefined function iconv_mime_encode() in /in/SYFeT:61 Stack trace: #0 /in/SYFeT(42): {closure}('"R\xC3\xA9al Nam\xC3\xA9" <...') #1 /in/SYFeT(53): mime_encoding_decoding_test('"R\xC3\xA9al Nam\xC3\xA9" <...', 'iconv_mime_enco...', Object(Closure)) #2 /in/SYFeT(61): {closure}('iconv_mime_enco...', Object(Closure)) #3 /in/SYFeT(76): execute_test('"R\xC3\xA9al Nam\xC3\xA9" <...') #4 {main} thrown in /in/SYFeT on line 61
Process exited with code 255.
Output for 7.0.0 - 7.0.33, 7.1.0 - 7.1.22, 7.2.0 - 7.2.10
Before encoding : "Réal Namé" <user@example.com> ----------------------------------------------------------------------------- After iconv_mime_encode Q : From: =?utf-8?Q?"R=C3=A9al=20Nam=C3=A9"=20<user?==?utf-8?Q?@exam?= =?utf-8?Q?ple.com>?= iconv_mime_decode : From: "Réal Namé" <user@example.com> mb_decode_mimeheader : From: "Réal Namé" <user@example.com> Fatal error: Uncaught Error: Call to undefined function imap_utf8() in /in/SYFeT:17 Stack trace: #0 [internal function]: {closure}(Array) #1 /in/SYFeT(17): preg_replace_callback('/=\\?[^?]+\\?(?:Q...', Object(Closure), 'From: =?utf-8?Q...') #2 /in/SYFeT(37): imap_utf8_wrapper('From: =?utf-8?Q...') #3 /in/SYFeT(44): decode_suite('From: =?utf-8?Q...') #4 /in/SYFeT(53): mime_encoding_decoding_test('From: =?utf-8?Q...', 'iconv_mime_enco...', Object(Closure)) #5 /in/SYFeT(61): {closure}('iconv_mime_enco...', Object(Closure)) #6 /in/SYFeT(76): execute_test('"R\xC3\xA9al Nam\xC3\xA9" <...') #7 {main} thrown in /in/SYFeT on line 17
Process exited with code 255.
Output for 5.6.0 - 5.6.40
Before encoding : "Réal Namé" <user@example.com> ----------------------------------------------------------------------------- After iconv_mime_encode Q : From: =?utf-8?Q?"R=C3=A9al=20Nam=C3=A9"=20<user?==?utf-8?Q?@exam?= =?utf-8?Q?ple.com>?= iconv_mime_decode : From: "Réal Namé" <user@example.com> mb_decode_mimeheader : From: "Réal Namé" <user@example.com> Fatal error: Call to undefined function imap_utf8() in /in/SYFeT on line 17
Process exited with code 255.
Output for 5.3.3 - 5.3.29, 5.5.0 - 5.5.38
Before encoding : "Réal Namé" <user@example.com> ----------------------------------------------------------------------------- After iconv_mime_encode Q : From: =?utf-8?Q?"R=C3=A9al=20Nam=C3=A9"=20<user?==?utf-8?Q?@exam?= =?utf-8?Q?ple.com>?= iconv_mime_decode : From: "Réal Namé" <user@example.com> mb_decode_mimeheader : From: "R�al Nam�" <user@example.com> Fatal error: Call to undefined function imap_utf8() in /in/SYFeT on line 17
Process exited with code 255.
Output for 5.4.0 - 5.4.45
Before encoding : "Réal Namé" <user@example.com> ----------------------------------------------------------------------------- Fatal error: Call to undefined function iconv_mime_encode() in /in/SYFeT on line 61
Process exited with code 255.
Output for 5.3.0 - 5.3.2
Before encoding : "Réal Namé" <user@example.com> ----------------------------------------------------------------------------- After iconv_mime_encode Q : From: =?utf-8?Q?"R=C3=A9al=20Nam=C3=A9"=20<user@example.com>?= iconv_mime_decode : From: "Réal Namé" <user@example.com> mb_decode_mimeheader : From: "R�al Nam�" <user@example.com> Fatal error: Call to undefined function imap_utf8() in /in/SYFeT on line 17
Process exited with code 255.
Output for 4.4.2 - 4.4.9, 5.1.0 - 5.1.6, 5.2.0 - 5.2.17
Parse error: syntax error, unexpected T_FUNCTION in /in/SYFeT on line 17
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.3.11, 4.4.0 - 4.4.1, 5.0.0 - 5.0.5
Parse error: parse error, unexpected T_FUNCTION in /in/SYFeT on line 17
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error in /in/SYFeT on line 17
Process exited with code 255.

preferences:
386.27 ms | 401 KiB | 461 Q