3v4l.org

run code in 300+ PHP versions simultaneously
<?php declare(strict_types=1); class Escaper { protected $encoding; public function __construct(string $encoding) { $this->encoding = $encoding; } public function js(string $input) : string { // json_encode can only accept UTF-8 encoded strings. if ($this->encoding !== 'UTF-8') { $input = mb_convert_encoding($input, 'UTF-8', $this->encoding); } $json = json_encode($input, JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT); if (!$json) { return ''; } $json = trim($json, '"'); // Convert back from UTF-8 if necessary. if ($this->encoding !== 'UTF-8') { //try { $json = mb_convert_encoding($json, $this->encoding, 'UTF-8'); //} catch (Error $e) { // echo 'ERROR CAUGHT. ENCODING: ' . $this->encoding; // return ''; //} } return $json; } } $malicious = 'alert("foo");</script>'; $malicious = 'test'; $utf = new Escaper('UTF-8'); $iso = new Escaper('ISO-8859-1'); echo $utf->js($malicious); echo "\n"; echo $iso->js($malicious); echo "\n"; foreach (mb_list_encodings() as $encoding) { $e = new Escaper($encoding); echo $e->js($malicious); echo ' (' . $encoding . ')'; echo "\n"; } /* mb_internal_encoding('ArmSCII-8'); mb_http_output('ArmSCII-8'); $e = new Escaper('ArmSCII-8'); echo $e->js($malicious); */
Output for 8.2.0 - 8.2.21, 8.3.0 - 8.3.9
test test (BASE64) Deprecated: mb_convert_encoding(): Handling Uuencode via mbstring is deprecated; use convert_uuencode/convert_uudecode instead in /in/l99ga on line 30 (UUENCODE) Deprecated: mb_convert_encoding(): Handling HTML entities via mbstring is deprecated; use htmlspecialchars, htmlentities, or mb_encode_numericentity/mb_decode_numericentity instead in /in/l99ga on line 30 test (HTML-ENTITIES) Deprecated: mb_convert_encoding(): Handling QPrint via mbstring is deprecated; use quoted_printable_encode/quoted_printable_decode instead in /in/l99ga on line 30 test (Quoted-Printable) test (7bit) test (8bit) ? (UCS-4) ? (UCS-4BE) ? (UCS-4LE) \u7465\u7374 (UCS-2) \u7465\u7374 (UCS-2BE) \u6574\u7473 (UCS-2LE) ? (UTF-32) ? (UTF-32BE) ? (UTF-32LE) \u7465\u7374 (UTF-16) \u7465\u7374 (UTF-16BE) \u6574\u7473 (UTF-16LE) test (UTF-8) test (UTF-7) test (UTF7-IMAP) test (ASCII) test (EUC-JP) test (SJIS) test (eucJP-win) test (EUC-JP-2004) test (SJIS-Mobile#DOCOMO) test (SJIS-Mobile#KDDI) test (SJIS-Mobile#SOFTBANK) test (SJIS-mac) test (SJIS-2004) test (UTF-8-Mobile#DOCOMO) test (UTF-8-Mobile#KDDI-A) test (UTF-8-Mobile#KDDI-B) test (UTF-8-Mobile#SOFTBANK) test (CP932) test (SJIS-win) test (CP51932) test (JIS) test (ISO-2022-JP) test (ISO-2022-JP-MS) test (GB18030) test (Windows-1252) test (Windows-1254) test (ISO-8859-1) test (ISO-8859-2) test (ISO-8859-3) test (ISO-8859-4) test (ISO-8859-5) test (ISO-8859-6) test (ISO-8859-7) test (ISO-8859-8) test (ISO-8859-9) test (ISO-8859-10) test (ISO-8859-13) test (ISO-8859-14) test (ISO-8859-15) test (ISO-8859-16) test (EUC-CN) test (CP936) test (HZ) test (EUC-TW) test (BIG-5) test (CP950) test (EUC-KR) test (UHC) $)Ctest (ISO-2022-KR) test (Windows-1251) test (CP866) test (KOI8-R) test (KOI8-U) test (ArmSCII-8) test (CP850) test (ISO-2022-JP-2004) test (ISO-2022-JP-MOBILE#KDDI) test (CP50220) test (CP50221) test (CP50222)
Output for 8.1.0 - 8.1.29
test test (BASE64) Fatal error: Uncaught TypeError: Escaper::js(): Return value must be of type string, bool returned in /in/l99ga:37 Stack trace: #0 /in/l99ga(55): Escaper->js('') #1 {main} thrown in /in/l99ga on line 37
Process exited with code 255.
Output for 8.0.0 - 8.0.30
test test test (wchar) \u7465\u7374 (byte2be) \u6574\u7473 (byte2le) ? (byte4be) ? (byte4le) (BASE64) Fatal error: Uncaught TypeError: Escaper::js(): Return value must be of type string, bool returned in /in/l99ga:37 Stack trace: #0 /in/l99ga(55): Escaper->js('') #1 {main} thrown in /in/l99ga on line 37
Process exited with code 255.
Output for 7.3.0 - 7.3.33, 7.4.0 - 7.4.33
test test Warning: mb_convert_encoding(): Unable to create character encoding converter in /in/l99ga on line 17 Warning: mb_convert_encoding(): Unable to create character encoding converter in /in/l99ga on line 30 Fatal error: Uncaught TypeError: Return value of Escaper::js() must be of the type string, bool returned in /in/l99ga:37 Stack trace: #0 /in/l99ga(55): Escaper->js(false) #1 {main} thrown in /in/l99ga on line 37
Process exited with code 255.
Output for 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33
test test test (pass) test (auto) test (wchar) \u7465\u7374 (byte2be) \u6574\u7473 (byte2le) ? (byte4be) ? (byte4le) (BASE64) (UUENCODE) test (HTML-ENTITIES) test (Quoted-Printable) test (7bit) test (8bit) ? (UCS-4) ? (UCS-4BE) ? (UCS-4LE) \u7465\u7374 (UCS-2) \u7465\u7374 (UCS-2BE) \u6574\u7473 (UCS-2LE) ? (UTF-32) ? (UTF-32BE) ? (UTF-32LE) \u7465\u7374 (UTF-16) \u7465\u7374 (UTF-16BE) \u6574\u7473 (UTF-16LE) test (UTF-8) test (UTF-7) test (UTF7-IMAP) test (ASCII) test (EUC-JP) test (SJIS) test (eucJP-win) test (EUC-JP-2004) test (SJIS-win) test (SJIS-Mobile#DOCOMO) test (SJIS-Mobile#KDDI) test (SJIS-Mobile#SOFTBANK) test (SJIS-mac) test (SJIS-2004) test (UTF-8-Mobile#DOCOMO) test (UTF-8-Mobile#KDDI-A) test (UTF-8-Mobile#KDDI-B) test (UTF-8-Mobile#SOFTBANK) test (CP932) test (CP51932) test (JIS) test (ISO-2022-JP) test (ISO-2022-JP-MS) test (GB18030) test (Windows-1252) test (Windows-1254) test (ISO-8859-1) test (ISO-8859-2) test (ISO-8859-3) test (ISO-8859-4) test (ISO-8859-5) test (ISO-8859-6) test (ISO-8859-7) test (ISO-8859-8) test (ISO-8859-9) test (ISO-8859-10) test (ISO-8859-13) test (ISO-8859-14) test (ISO-8859-15) test (ISO-8859-16) test (EUC-CN) test (CP936) test (HZ) test (EUC-TW) test (BIG-5) test (CP950) test (EUC-KR) test (UHC) test (ISO-2022-KR) test (Windows-1251) test (CP866) test (KOI8-R) test (KOI8-U) test (ArmSCII-8) test (CP850) test (JIS-ms) test (ISO-2022-JP-2004) test (ISO-2022-JP-MOBILE#KDDI) test (CP50220) test (CP50220raw) test (CP50221) test (CP50222)
Output for 5.6.0 - 5.6.40
Warning: Unsupported declare 'strict_types' in /in/l99ga on line 2 Parse error: syntax error, unexpected ':', expecting ';' or '{' in /in/l99ga on line 13
Process exited with code 255.

preferences:
184.73 ms | 403 KiB | 303 Q