Core\Bytes::indexOf
Core\Bytes::indexOf(bytes $haystack, bytes $needle, {from?: int}): ?uintCore\Bytes::indexOf( bytes $haystack, bytes $needle, {from?: int}, ): ?uint
Finds where one bytes value first occurs inside another, and gives you the position as a number of bytes.
The position is counted from the start of the value, and the first byte is at position 0. When the bytes you are looking for are not there, the result is null. The from option starts the search at a later position, and a negative from counts back from the end of the value. A position past either end of the value is moved to that end, so a search never fails because of the number you gave it.
The position you get back is the same kind of number Core\Bytes::slice takes, so you can cut the value at the place a search found. Use Core\Bytes::contains instead when you only need to know whether the bytes are there.
Good to know: a value that is not found gives you null and never false, so a match at position 0 can never be mistaken for "not found".
Parameters
$haystackbytesThe buffer searched.$needlebytesThe bytes to find. An empty needle is found where the search starts.
Options
fromintoptionalThe byte offset where the search starts. A negative offset counts from the end. The default is0.
Return value
Returns ?uint. The byte offset of the first match at or after from. When there is no match, the result is null.
Examples
Finding a marker in a buffer
<?nvs// Shows how to find where some bytes start inside a larger value.// `Core\Bytes::indexOf` returns the position of the first match, or `null`// when the bytes are not there.
bytes $frame = Core\Encoding::fromHex('00ff00ff10203000');
// The two bytes 16 and 32 start at position 4. This prints 4.var $at = Core\Bytes::indexOf($frame, Core\Encoding::fromHex('1020'));if ($at != null) { echo $at, "\n";}
// These two bytes are nowhere in the value, so the result is `null`.// This prints "not found".if (Core\Bytes::indexOf($frame, Core\Encoding::fromHex('abcd')) == null) { echo "not found\n";}4not foundSearching from a later position
<?nvs// Shows how to search from a later position. The `from` option says where the// search starts. A negative `from` counts back from the end of the value.
bytes $line = 'name=one&name=two' as bytes;bytes $needle = 'name=' as bytes;
// Without `from`, the search starts at the beginning. This prints 0.echo Core\Bytes::indexOf($line, $needle) ?? 0, "\n";
// Starting at position 1 skips the first match and finds the second one.// This prints 9.echo Core\Bytes::indexOf($line, $needle, { from: 1 }) ?? 0, "\n";
// A `from` of -8 starts the search 8 bytes before the end. This prints 9.echo Core\Bytes::indexOf($line, $needle, { from: -8 }) ?? 0, "\n";099Cutting a message at its header
<?nvs// Shows how a program splits a message it read from a network connection into// its header and its body. The header ends with an empty line, which is the// four bytes 13, 10, 13, 10.
final class Message{ // Returns the body of the message. The body is empty while the header has // not arrived in full. public static function body(bytes $received): bytes { var $at = Core\Bytes::indexOf($received, Core\Encoding::fromHex('0d0a0d0a')); if ($at == null) { return Core\Encoding::fromHex(''); } // The search returns a byte position and `Core\Bytes::slice` takes // one. The body starts 4 bytes after the empty line begins. return Core\Bytes::slice($received, ($at as int) + 4); }}
bytes $whole = "POST /orders HTTP/1.1\r\nHost: example.com\r\n\r\nid=1042" as bytes;bytes $partial = "POST /orders HTTP/1.1\r\nHost: example.com\r\n" as bytes;
// This prints "id=1042".echo Core\Encoding::decodeText(Message::body($whole), Core\Charset::Utf8), "\n";
// The header is not finished, so there is no body yet. This prints 0.echo Core\Bytes::length(Message::body($partial)), "\n";id=10420