3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?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); */
based on FKekf
Output for 7.0.0 - 7.3.0beta1
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.26
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.