forked from azjezz/psl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontains.php
46 lines (42 loc) · 1.07 KB
/
contains.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<?php
declare(strict_types=1);
namespace Psl\Str;
/**
* Returns whether the 'haystack' string contains the 'needle' string.
*
* An optional offset determines where in the haystack the search begins.
*
* If the offset is negative, the search will begin that many characters from the end
* of the string.
*
* Example:
*
* Str\contains('hello', 'l')
* => Bool(true)
*
* Str\contains('Hello, 'h')
* => Bool(false)
*
* Str\contains('hello', 'L', 3)
* => Bool(false)
*
* Str\contains('hello', 'l', 4)
* => Bool(false)
*
* Str\contains('hello', 'l', 2)
* => Bool(true)
*
* Str\contains('سيف', 'س')
* => Bool(true)
*
* @pure
*
* @throws Exception\OutOfBoundsException If the $offset is out-of-bounds.
*/
function contains(string $haystack, string $needle, int $offset = 0, Encoding $encoding = Encoding::UTF_8): bool
{
if ('' === $needle) {
return Internal\validate_offset($offset, length($haystack, $encoding), true);
}
return null !== search($haystack, $needle, $offset, $encoding);
}