Core\Bytes::startsWith
Core\Bytes::startsWith(bytes $b, bytes $prefix): boolCore\Bytes::startsWith( bytes $b, bytes $prefix, ): bool
Tells you whether a bytes value begins with some other bytes.
The result is true when the first bytes of the value are exactly the bytes you gave, and false when they are not. The comparison reads the bytes one after the other. There is no option to ignore the difference between upper case and lower case, because a bytes value carries no language and no character set.
This is the method a program writes when it looks at the first few bytes of a file to decide what kind of file it is. Those first bytes are called a magic number, and every common format has one. Use Core\Bytes::indexOf instead when the bytes may be anywhere in the value.
Good to know: an empty value begins every value, so bytes with nothing in them always return true.
Parameters
$bbytesThe buffer.$prefixbytesThe bytes$bmust begin with.
Return value
Returns bool. true when $b begins with $prefix. An empty prefix matches every buffer.
Examples
Checking the first bytes of a file
<?nvs// Shows how to check what kind of file a value holds. A PNG image always// starts with the same eight bytes.
bytes $pngMagic = Core\Encoding::fromHex('89504e470d0a1a0a');bytes $image = Core\Encoding::fromHex('89504e470d0a1a0a0000000d49484452');
// The image starts with those eight bytes, so this prints "yes".echo Core\Bytes::startsWith($image, $pngMagic) ? 'yes' : 'no', "\n";
// A GIF image starts with other bytes, so this prints "no".echo Core\Bytes::startsWith('GIF89a' as bytes, $pngMagic) ? 'yes' : 'no', "\n";yesnoAn empty prefix and one too long
<?nvs// Shows the two edges of the test. An empty value begins every value, and// bytes longer than the value can never begin it.
bytes $name = 'report.pdf' as bytes;
// This prints "true".echo Core\Bytes::startsWith($name, '' as bytes) ? 'true' : 'false', "\n";
// The value is 10 bytes long, so 11 bytes cannot begin it. This prints "false".echo Core\Bytes::startsWith($name, 'report.pdfx' as bytes) ? 'true' : 'false', "\n";
// The bytes have to be at the very start. This prints "false".echo Core\Bytes::startsWith($name, 'pdf' as bytes) ? 'true' : 'false', "\n";truefalsefalseNaming the format of an upload
<?nvs// Shows how a program that accepts uploads decides what it was given. It// compares the first bytes of the upload against the formats it allows.
final class Upload{ // Returns the name of the format, or "unknown" when none of them matches. public static function format(bytes $content): string { if (Core\Bytes::startsWith($content, Core\Encoding::fromHex('89504e470d0a1a0a'))) { return 'png'; } if (Core\Bytes::startsWith($content, 'GIF89a' as bytes)) { return 'gif'; } if (Core\Bytes::startsWith($content, '%PDF-' as bytes)) { return 'pdf'; } return 'unknown'; }}
// This prints "pdf" and then "unknown".echo Upload::format("%PDF-1.7\n1 0 obj" as bytes), "\n";echo Upload::format('dear support, please help' as bytes), "\n";pdfunknown