Core\Bytes::contains
Core\Bytes::contains(bytes $haystack, bytes $needle): boolCore\Bytes::contains( bytes $haystack, bytes $needle, ): bool
Tells you whether one bytes value occurs anywhere inside another.
The result is true when the bytes you are looking for appear somewhere in the value you are searching, and false when they do not. The search matches bytes exactly, 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. Use Core\Bytes::indexOf instead when you also need the position.
Good to know: an empty value occurs in everything, so searching for one always returns true.
Parameters
$haystackbytesThe buffer searched.$needlebytesThe bytes to find.
Return value
Returns bool. true when $needle occurs in $haystack. An empty needle is in every buffer, also in an empty one.
Examples
Looking for bytes inside a value
<?nvs// Shows how to test whether one `bytes` value occurs inside another.
bytes $body = 'name=ada&role=admin' as bytes;
// "role=" occurs in the body, so this prints "true".echo Core\Bytes::contains($body, 'role=' as bytes) ? 'true' : 'false', "\n";
// "token=" does not occur, so this prints "false".echo Core\Bytes::contains($body, 'token=' as bytes) ? 'true' : 'false', "\n";
// The bytes must occur one after the other. "adaadmin" does not, so this// prints "false".echo Core\Bytes::contains($body, 'adaadmin' as bytes) ? 'true' : 'false', "\n";truefalsefalseExact bytes and the empty value
<?nvs// Shows two rules of the search: the bytes must match exactly, and an empty// value occurs everywhere.
bytes $data = 'Content-Type' as bytes;
// Upper case and lower case are different bytes, so this prints "false".echo Core\Bytes::contains($data, 'content' as bytes) ? 'true' : 'false', "\n";
// The same letters in the same case are found. This prints "true".echo Core\Bytes::contains($data, 'Content' as bytes) ? 'true' : 'false', "\n";
// An empty value occurs in every value, including in an empty one. This// prints "true" twice.echo Core\Bytes::contains($data, '' as bytes) ? 'true' : 'false', "\n";echo Core\Bytes::contains('' as bytes, '' as bytes) ? 'true' : 'false', "\n";falsetruetruetrueWaiting for a whole message
<?nvs// Shows how a program reading from a network connection knows that the headers// of a message have all arrived. The headers end with an empty line, which is// the four bytes 13, 10, 13, 10.
class Reader{ // Returns true when the headers are complete in what has arrived so far. public static function headersComplete(bytes $received): bool { return Core\Bytes::contains($received, Core\Encoding::fromHex('0d0a0d0a')); }}
bytes $partial = "GET / HTTP/1.1\r\nHost: example.com\r\n" as bytes;bytes $whole = "GET / HTTP/1.1\r\nHost: example.com\r\n\r\n" as bytes;
// This prints "wait" and then "ready".echo Reader::headersComplete($partial) ? 'ready' : 'wait', "\n";echo Reader::headersComplete($whole) ? 'ready' : 'wait', "\n";waitready