Core\Encoding::encodeText
Core\Encoding::encodeText(string $s, Core\Charset $charset): bytesCore\Encoding::encodeText( string $s, Core\Charset $charset, ): bytes
Writes text as bytes, in the character encoding you name.
A file, a network message or an older system expects its text in one particular encoding. Core\Encoding::encodeText turns a string into the bytes of that encoding: Core\Charset::Utf8, Core\Charset::Windows1252, Core\Charset::ShiftJis, and every other encoding the web standard names. The conversion is exact, so the bytes always spell the text you gave. Most encodings have no spelling for most characters. If your text holds one of them, the method throws a RuntimeError that names the character and where it sits, and you can then pick a different encoding or a different character.
Good to know: Core\Encoding::decodeText is the other direction. UTF-8 and UTF-16 can write every character there is; every other encoding covers only part of them.
Parameters
$sstringThe text to encode.$charsetCore\CharsetThe encoding to write.
Return value
Returns bytes. The encoded bytes.
Errors
RuntimeError$charsetcannot represent a character of$s. The message gives the character and its offset.
Examples
One text three encodings
<?nvs// Shows how to write text as bytes. The encoding you name decides how many// bytes each character takes, and which bytes they are.
string $accented = 'é';
// UTF-8 writes this character as two bytes. This prints c3a9.echo Core\Encoding::toHex(Core\Encoding::encodeText($accented, Core\Charset::Utf8)), "\n";
// ISO-8859-1 has a single byte for it. This prints e9.echo Core\Encoding::toHex(Core\Encoding::encodeText($accented, Core\Charset::Latin1)), "\n";
// UTF-16 always uses two bytes per character, smallest byte first here. This// prints e900.echo Core\Encoding::toHex(Core\Encoding::encodeText($accented, Core\Charset::Utf16Le)), "\n";
// Japanese text in Shift_JIS, the encoding many Japanese systems still use.// This prints 82a082a2.echo Core\Encoding::toHex(Core\Encoding::encodeText('あい', Core\Charset::ShiftJis)), "\n";c3a9e9e90082a082a2A character the encoding cannot write
<?nvs// Shows what happens when the encoding has no spelling for one of your// characters. The result is an error naming that character, so the bytes never// hold a "?" where the text had something else.
// ISO-8859-1 covers western European letters and not the euro sign.try { Core\Encoding::encodeText('Preis: 10 €', Core\Charset::Latin1);} catch (RuntimeError $noSpelling) { echo 'could not write: ', $noSpelling->message, "\n";}
// windows-1252 is the same table with the euro sign added, and writes it as// the single byte 80. This prints 50726569733a2031302080.echo Core\Encoding::toHex( Core\Encoding::encodeText('Preis: 10 €', Core\Charset::Windows1252),), "\n";
// UTF-8 can write every character there is. The euro sign takes three bytes// there. This prints 50726569733a20313020e282ac.echo Core\Encoding::toHex( Core\Encoding::encodeText('Preis: 10 €', Core\Charset::Utf8),), "\n";could not write: Core\Encoding::encodeText(): `Core\Charset::Latin1` has no spelling for '€' (U+20AC) at offset 10 of "Preis: 10 €" — it is exact or it throws, and there is no transliterating or ignoring mode50726569733a203130208050726569733a20313020e282acA file an older system can read
<?nvs// Shows the everyday use: your program keeps text as UTF-8, and it has to// hand a file to an accounting system that only reads windows-1252.
array<string> $names = ['Müller', 'Größe', 'Sørensen', 'Ceccherini'];
// Check each name before writing the file. `Core\Charset::Windows1252` covers// western European letters, so this prints 4 of 4 names fit.uint $fit = 0;foreach ($names as string $name) { try { Core\Encoding::encodeText($name, Core\Charset::Windows1252); $fit = $fit + 1; } catch (RuntimeError $noSpelling) { echo 'cannot write: ', $name, "\n"; }}echo $fit, ' of ', Core\Arr::count($names), " names fit\n";
// Build the line the old system expects. The hex starts with 4dfc6c6c6572,// which is Müller: one byte for the ü.string $line = Core\Str::join($names, ';');bytes $encoded = Core\Encoding::encodeText($line, Core\Charset::Windows1252);echo Core\Encoding::toHex($encoded), "\n";
// The same text as UTF-8 takes more room, because each of the four accented// letters is two bytes there and one byte here. This prints 32 and 36.bytes $asUtf8 = Core\Encoding::encodeText($line, Core\Charset::Utf8);echo Core\Bytes::length($encoded), ' and ', Core\Bytes::length($asUtf8), "\n";4 of 4 names fit4dfc6c6c65723b4772f6df653b53f872656e73656e3b43656363686572696e6932 and 36