Skip to content

Core\Encoding::isValidText

Tells you whether bytes are valid text, in the character encoding you name.

Bytes that arrive from a file, an upload or a network message are not always text. Core\Encoding::isValidText reads them in the encoding you name. It returns true when every byte sequence is one that encoding allows, and false when one of them is not. It never throws an error, so you can put it in an if and decide yourself what a bad input should do.

This is the same test Core\Encoding::decodeText makes before it converts. Use isValidText when your program has something to do with a bad input, such as skipping a row or trying another encoding. Use decodeText inside a try when a bad input is an error.

Good to know: the answer is always true for Core\Charset::Latin1. That encoding gives all 256 bytes a meaning, so no bytes are invalid in it.

Parameters

  • $bbytesThe bytes to check.
  • $charsetCore\CharsetThe encoding to check the bytes against.

Return value

Returns bool. true when decodeText would succeed, false otherwise. With Charset::Latin1 the result is always true, because every one of the 256 byte values is valid.

Examples

Are these bytes text

<?nvs
// Shows how to test whether bytes are text before you read them.
array<bytes> $buffers = [
// "hello" written as UTF-8.
Core\Encoding::fromHex('68656c6c6f'),
// The same five bytes, with one byte that no UTF-8 character starts with.
Core\Encoding::fromHex('68656cff6f'),
];
// The test returns `true` or `false` and never throws an error.
// This prints yes and then no.
foreach ($buffers as bytes $buffer) {
if (Core\Encoding::isValidText($buffer, Core\Charset::Utf8)) {
echo "yes\n";
} else {
echo "no\n";
}
}
Output
yes
no

Which encoding wrote this

<?nvs
// Shows how to find the encoding a file was written in, by testing the bytes
// against each encoding your program accepts.
// One line of an export. It is not valid UTF-8, so something else wrote it.
bytes $line = Core\Encoding::fromHex('4dfc6c6c6572');
// The encodings this program accepts, the most likely one first.
array<Core\Charset> $accepted = [Core\Charset::Utf8, Core\Charset::Windows1252];
foreach ($accepted as Core\Charset $charset) {
// Stop at the first encoding that reads the whole line. This prints
// Müller.
if (Core\Encoding::isValidText($line, $charset)) {
echo Core\Encoding::decodeText($line, $charset), "\n";
break;
}
}
Output
Müller

Keep the good rows of an upload

<?nvs
// Shows the everyday use: checking bytes that come from outside your program,
// and keeping only the ones you can read.
// Three fields as they arrive from an upload. The middle one ends in the
// middle of a character.
array<bytes> $fields = [
Core\Encoding::fromHex('4f726465722031303432'),
Core\Encoding::fromHex('4f7264657220c3'),
Core\Encoding::fromHex('4f726465722031303433'),
];
array<string> $kept = [];
uint $rejected = 0;
foreach ($fields as bytes $field) {
// A field that is not valid UTF-8 is counted and left out. Nothing throws
// an error, so one bad field does not stop the other two.
if (Core\Encoding::isValidText($field, Core\Charset::Utf8)) {
$kept[] = Core\Encoding::decodeText($field, Core\Charset::Utf8);
} else {
$rejected = $rejected + 1;
}
}
// This prints 2 kept, 1 rejected.
echo Core\Arr::count($kept), ' kept, ', $rejected, " rejected\n";
Output
2 kept, 1 rejected