Skip to content

Core\Bytes::slice

Copies a part of a bytes value into a new value.

The part starts at the position you give, counted in bytes, and the first byte is at position 0. A negative position counts from the end of the value, so -4 starts at the fourth byte from the end. The length says how many bytes to take. A negative length stops that many bytes short of the end. When you leave the length out, the part runs to the end of the value.

The value you pass stays as it is. A position after the end of the value returns a value with nothing in it, and so does a length of 0.

Good to know: the positions count bytes, not characters. Use Core\Str::slice for text that a person reads, where one character can be several bytes.

Parameters

  • $bbytesThe buffer.
  • $offsetintWhere the part starts. A negative offset counts from the end. An offset before the start is treated as the start.
  • $length?intdefault nullHow many bytes to take. A negative length stops that many bytes before the end. The default is null, which takes everything to the end.

Return value

Returns bytes. The part as a new buffer. The result is empty when the offset is past the end, or when the part would end before it starts.

Examples

Taking a part of a value

<?nvs
// Shows how to copy a part of a `bytes` value. The part starts at a position
// and has a length, both counted in bytes.
bytes $record = '2026-09-21T08:15:00Z' as bytes;
// 4 bytes from position 0. This prints "2026".
echo Core\Bytes::slice($record, 0, 4) as string, "\n";
// Without a length the part runs to the end. This prints "08:15:00Z".
echo Core\Bytes::slice($record, 11) as string, "\n";
Output
2026
08:15:00Z

Positions that count from the end

<?nvs
// Shows the positions that count from the end of a value. A negative position
// starts there, and a negative length stops there.
bytes $record = '2026-09-21T08:15:00Z' as bytes;
// The last byte is at position -1. This prints "Z".
echo Core\Bytes::slice($record, -1) as string, "\n";
// A length of -1 stops one byte before the end. This prints
// "2026-09-21T08:15:00".
echo Core\Bytes::slice($record, 0, -1) as string, "\n";
// A position after the end gives a value with nothing in it. This prints 0.
echo Core\Bytes::length(Core\Bytes::slice($record, 99)), "\n";
Output
Z
2026-09-21T08:15:00
0

Splitting a message into header and body

<?nvs
// Shows how a program reads a message that begins with a header of a fixed
// size. Here the first 8 bytes are the header and the rest is the text.
final class Message
{
// Returns the 8 bytes at the front of the message.
public static function header(bytes $frame): bytes
{
return Core\Bytes::slice($frame, 0, 8);
}
// Returns everything after those 8 bytes.
public static function body(bytes $frame): bytes
{
return Core\Bytes::slice($frame, 8);
}
}
bytes $frame = 'MSG:0042Hello there' as bytes;
// This prints "MSG:0042".
echo Message::header($frame) as string, "\n";
// This prints "Hello there".
echo Message::body($frame) as string, "\n";
Output
MSG:0042
Hello there