3v4l.org

run code in 300+ PHP versions simultaneously
<?php // Sample generation $sampleUTF8 = 'Here is some lovely Russian: На берегу пустынных волн Стоял он, дум великих полн, И вдаль глядел. Пред ним широко Река неслася; бедный чёлн По ней стремился одиноко. and something else: ԠѬΏݑޤ'; $sampleUTF16 = mb_convert_encoding($sampleUTF8, 'UTF-16', 'UTF-8'); /** * Detect Encoding * Attempts to identify the encoding of the subject string; returns the encoding as a string. * * @param string $subject * * @return string * @throws \RuntimeException Encoding could not be identified */ function detect(string $subject): string { if (mb_detect_encoding($subject, 'auto')) { return mb_detect_encoding($subject, 'ASCII, UTF-8, UTF-7, UTF-16, UTF-16BE, UTF-16LE, UTF-32, UTF-32BE, UTF-32LE, SJIS, JIS, ISO-8859-1, ISO-8859-2, ISO-8859-3, ISO-8859-4, ISO-8859-5, ISO-8859-6, ISO-8859-7, ISO-8859-8, ISO-8859-9, ISO-8859-10, ISO-8859-11, ISO-8859-12, ISO-8859-13, ISO-8859-14, ISO-8859-15, Windows-1251, Windows-1252, Windows-1254, BASE-64, UCS-2, UCS-2BE, UCS-2LE, UCS-4, UCS-4BE, UCS-4LE, CP51932, CP50222, CP50221, CP50220, CP950, CP936, CP932, CP866, CP850, auto', true); } else { throw new \RuntimeException('Could not identify the string\'s encoding.'); } } echo detect($sampleUTF16);
Output for 8.0.0 - 8.0.30, 8.3.0 - 8.3.26, 8.4.1 - 8.4.13
Fatal error: Uncaught ValueError: mb_detect_encoding(): Argument #2 ($encodings) contains invalid encoding "ISO-8859-11" in /in/OZqmh:20 Stack trace: #0 /in/OZqmh(20): mb_detect_encoding('\x00H\x00e\x00r\x00e\x00 \x00i\x00s\x00...', 'ASCII, UTF-8, U...', true) #1 /in/OZqmh(28): detect('\x00H\x00e\x00r\x00e\x00 \x00i\x00s\x00...') #2 {main} thrown in /in/OZqmh on line 20
Process exited with code 255.
Output for 8.1.0 - 8.1.33, 8.2.0 - 8.2.29
Fatal error: Uncaught RuntimeException: Could not identify the string's encoding. in /in/OZqmh:22 Stack trace: #0 /in/OZqmh(28): detect('\x00H\x00e\x00r\x00e\x00 \x00i\x00s\x00...') #1 {main} thrown in /in/OZqmh on line 22
Process exited with code 255.
Output for 7.2.29 - 7.2.33, 7.3.16 - 7.3.33, 7.4.0 - 7.4.33
Warning: mb_detect_encoding(): Illegal argument in /in/OZqmh on line 20
Output for 7.0.0 - 7.0.20, 7.1.0 - 7.1.20, 7.2.6
ISO-8859-1
Output for 5.5.0 - 5.5.35, 5.6.0 - 5.6.28
Parse error: syntax error, unexpected ':', expecting '{' in /in/OZqmh on line 17
Process exited with code 255.

preferences:
133.23 ms | 412 KiB | 5 Q