1
|
|
|
<?php namespace Gears\String\Methods; |
2
|
|
|
//////////////////////////////////////////////////////////////////////////////// |
3
|
|
|
// __________ __ ________ __________ |
4
|
|
|
// \______ \ |__ ______ / _____/ ____ _____ ______\______ \ _______ ___ |
5
|
|
|
// | ___/ | \\____ \/ \ ____/ __ \\__ \\_ __ \ | _// _ \ \/ / |
6
|
|
|
// | | | Y \ |_> > \_\ \ ___/ / __ \| | \/ | ( <_> > < |
7
|
|
|
// |____| |___| / __/ \______ /\___ >____ /__| |______ /\____/__/\_ \ |
8
|
|
|
// \/|__| \/ \/ \/ \/ \/ |
9
|
|
|
// ----------------------------------------------------------------------------- |
10
|
|
|
// Designed and Developed by Brad Jones <brad @="bjc.id.au" /> |
11
|
|
|
// ----------------------------------------------------------------------------- |
12
|
|
|
//////////////////////////////////////////////////////////////////////////////// |
13
|
|
|
|
14
|
|
|
use voku\helper\UTF8; |
15
|
|
|
|
16
|
|
|
trait IndexOf |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Returns the index of the first occurrence of $needle in the string, |
20
|
|
|
* and false if not found. Accepts an optional offset from which to begin |
21
|
|
|
* the search. |
22
|
|
|
* |
23
|
|
|
* @param string $needle Substring to look for. |
24
|
|
|
* |
25
|
|
|
* @param int $offset Offset from which to search. |
26
|
|
|
* |
27
|
|
|
* @return int|bool The occurrence's index if found, |
28
|
|
|
* otherwise false. |
29
|
|
|
*/ |
30
|
|
|
public function indexOf($needle, $offset = 0) |
31
|
|
|
{ |
32
|
|
|
return UTF8::strpos |
33
|
|
|
( |
34
|
|
|
$this->scalarString, |
35
|
|
|
(string)$needle, |
36
|
|
|
(int)$offset, |
37
|
|
|
$this->encoding |
38
|
|
|
); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Returns the index of the last occurrence of $needle in the string, |
43
|
|
|
* and false if not found. Accepts an optional offset from which to begin |
44
|
|
|
* the search. Offsets may be negative to count from the last character |
45
|
|
|
* in the string. |
46
|
|
|
* |
47
|
|
|
* @param string $needle Substring to look for. |
48
|
|
|
* |
49
|
|
|
* @param int $offset Offset from which to search. |
50
|
|
|
* |
51
|
|
|
* @return int|bool The last occurrence's index if found, |
52
|
|
|
* otherwise false. |
53
|
|
|
*/ |
54
|
|
|
public function indexOfLast($needle, $offset = 0) |
55
|
|
|
{ |
56
|
|
|
return UTF8::strrpos |
57
|
|
|
( |
58
|
|
|
$this->scalarString, |
59
|
|
|
(string)$needle, |
60
|
|
|
(int)$offset, |
61
|
|
|
$this->encoding |
62
|
|
|
); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|