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);

preferences:
102.46 ms | 1669 KiB | 5 Q