Core\Encoding::decodeText
Core\Encoding::decodeText(bytes $b, Core\Charset $charset): stringCore\Encoding::decodeText( bytes $b, Core\Charset $charset, ): string
Reads bytes as text, in the character encoding you name.
A file, a network message or a database column arrives as bytes. Core\Encoding::decodeText turns those bytes into a string, using the encoding you say they are written in: Core\Charset::Utf8, Core\Charset::Windows1252, Core\Charset::ShiftJis, and every other encoding the web standard names. The conversion is exact, so the result is always the text that was sent. If the bytes are not valid in that encoding, the method throws a RuntimeError that names the position of the first byte it could not read.
Good to know: Core\Encoding::isValidText tests the same thing and returns true or false. Use it when you want to choose what to do instead of catching an error.
Parameters
$bbytesThe bytes to decode.$charsetCore\CharsetThe encoding the bytes are in.
Return value
Returns string. The decoded text. Empty bytes give the empty string.
Errors
RuntimeErrorA byte sequence in$bis not valid in$charset. The message gives its offset.
Examples
The encoding decides what the bytes mean
<?nvs// Shows how to read bytes as text. The same bytes give different text under// different encodings, so you name the one the bytes are written in.
// The two bytes that spell "é" in UTF-8.bytes $accented = Core\Encoding::fromHex('c3a9');
// Read as UTF-8 they are one character. This prints é.echo Core\Encoding::decodeText($accented, Core\Charset::Utf8), "\n";
// ISO-8859-1 gives every single byte its own character, so the same two bytes// are two characters there. This prints é.echo Core\Encoding::decodeText($accented, Core\Charset::Latin1), "\n";
// One byte can also be a character. In windows-1252 the byte 0x80 is the euro// sign. This prints €.bytes $high = Core\Encoding::fromHex('80');echo Core\Encoding::decodeText($high, Core\Charset::Windows1252), "\n";éé€Bytes that are not valid text
<?nvs// Shows what happens when the bytes are not valid in the encoding you named.// The result is an error, so a broken byte never ends up hidden in your text.
// A single byte 0xFF. No UTF-8 character starts with it.bytes $broken = Core\Encoding::fromHex('ff');
try { echo Core\Encoding::decodeText($broken, Core\Charset::Utf8), "\n";} catch (RuntimeError $notUtf8) { // The message names the position of the first byte that could not be read. echo 'could not read: ', $notUtf8->message, "\n";}
// `Core\Encoding::isValidText` tests the same thing and returns a bool. Use it// when you want to choose what to do. This prints no.echo (Core\Encoding::isValidText($broken, Core\Charset::Utf8) ? 'yes' : 'no'), "\n";
// The same byte is valid in ISO-8859-1, where it spells "ÿ". This prints yes.echo (Core\Encoding::isValidText($broken, Core\Charset::Latin1) ? 'yes' : 'no'), "\n";could not read: a conversion is exact or it throws: Core\Encoding::decodeText() found at offset 0 a byte sequence that is not `Core\Charset::Utf8`, and nothing is replaced with U+FFFD or droppednoyesA file from an older system
<?nvs// Shows the everyday use: an older system exports its data in windows-1252,// and your program keeps text as UTF-8.
// One line of that export, as it arrives from the file.bytes $line = Core\Encoding::fromHex('4dfc6c6c65723b4772f6df65');
// Read the bytes as the encoding the old system wrote. This prints// Müller;Größe.string $text = Core\Encoding::decodeText($line, Core\Charset::Windows1252);echo $text, "\n";
// It is now a normal string, so every `Core\Str` method works on it. This// prints 12, the number of characters a person would count.echo Core\Str::length($text), "\n";
// Write it back out as UTF-8 for a file, a response or a database column.// This prints 4dc3bc6c6c65723b4772c3b6c39f65.echo Core\Encoding::toHex(Core\Encoding::encodeText($text, Core\Charset::Utf8)), "\n";Müller;Größe124dc3bc6c6c65723b4772c3b6c39f65