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
|
|
|
use Gears\String\Exceptions\PcreException; |
16
|
|
|
|
17
|
|
|
trait Between |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Returns the substring between `$start` and `$end`. |
21
|
|
|
* |
22
|
|
|
* An optional offset may be supplied from which |
23
|
|
|
* to begin the search for the start string. |
24
|
|
|
* |
25
|
|
|
* @param string $start Delimiter marking the start of the substring. |
26
|
|
|
* |
27
|
|
|
* @param string $end Delimiter marking the end of the substring. |
28
|
|
|
* |
29
|
|
|
* @param int $offset Index from which to begin the search. |
30
|
|
|
* |
31
|
|
|
* @param bool $include If true, we include the start & end in the result. |
32
|
|
|
* |
33
|
|
|
* @return static Str object between $start & $end. |
34
|
|
|
*/ |
35
|
|
|
public function between($start, $end, $offset = 0, $include = false) |
36
|
|
|
{ |
37
|
|
|
$startIndex = $this->indexOf($start, $offset); |
38
|
|
|
if ($startIndex === false) return $this->newSelf(''); |
39
|
|
|
|
40
|
|
|
$substrIndex = $startIndex + UTF8::strlen($start, $this->encoding); |
41
|
|
|
|
42
|
|
|
$endIndex = $this->indexOf($end, $substrIndex); |
43
|
|
|
if ($endIndex === false) return $this->newSelf(''); |
44
|
|
|
|
45
|
|
|
$result = UTF8::substr |
46
|
|
|
( |
47
|
|
|
$this->scalarString, |
48
|
|
|
$substrIndex, |
49
|
|
|
$endIndex - $substrIndex, |
50
|
|
|
$this->encoding |
51
|
|
|
); |
52
|
|
|
|
53
|
|
|
if ($include === true) $result = $start.$result.$end; |
54
|
|
|
|
55
|
|
|
return $this->newSelf($result); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Returns an array of substrings between $start and $end. |
60
|
|
|
* |
61
|
|
|
* @param string $start Delimiter marking the start of the substring. |
62
|
|
|
* |
63
|
|
|
* @param string $end Delimiter marking the end of the substring. |
64
|
|
|
* |
65
|
|
|
* @return static[] |
66
|
|
|
* |
67
|
|
|
* @throws PcreException When PCRE Error occurs. |
68
|
|
|
*/ |
69
|
|
|
public function betweenAll($start, $end) |
70
|
|
|
{ |
71
|
|
|
$matches = []; |
72
|
|
|
|
73
|
|
|
// Create the regular expression |
74
|
|
|
$find = '/'.preg_quote($start, '/').'(.*?)'.preg_quote($end, '/').'/su'; |
75
|
|
|
|
76
|
|
|
// Run the regular expression |
77
|
|
|
if (preg_match_all($find, $this->scalarString, $matches) !== false) |
78
|
|
|
{ |
79
|
|
|
return $this->newSelfs($matches); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
// Throw with the last known pcre error code. |
83
|
|
|
throw new PcreException(); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|