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 Regx |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* The delimiter we will use for all regular expressions. |
21
|
|
|
* |
22
|
|
|
* @link http://php.net/manual/en/regexp.reference.delimiters.php |
23
|
|
|
* |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
protected $regexDelimiter = '/'; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Allows you to change the the default regular expression delimiter. |
30
|
|
|
* |
31
|
|
|
* @param string $value The delimiter to use for all future expressions. |
32
|
|
|
* |
33
|
|
|
* @return static |
34
|
|
|
*/ |
35
|
|
|
public function setRegexDelimiter($value) |
36
|
|
|
{ |
37
|
|
|
$this->regexDelimiter = $value; |
38
|
|
|
|
39
|
|
|
return $this; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Returns true if the string matches the supplied pattern, false otherwise. |
44
|
|
|
* |
45
|
|
|
* @param string $pattern Regex pattern to match against. |
46
|
|
|
* |
47
|
|
|
* @param string $options Matching conditions to be used. |
48
|
|
|
* |
49
|
|
|
* @return bool |
50
|
|
|
* |
51
|
|
|
* @throws PcreException When PCRE Error occurs. |
52
|
|
|
*/ |
53
|
|
|
public function regexMatch($pattern, $options = '') |
54
|
|
|
{ |
55
|
|
|
// Ensure the options contain the "u" modifier. |
56
|
|
|
if (!$this->newSelf($options)->contains('u')) $options .= 'u'; |
57
|
|
|
|
58
|
|
|
// Build the expression |
59
|
|
|
$expression = |
60
|
|
|
$this->regexDelimiter. |
61
|
|
|
$pattern. |
62
|
|
|
$this->regexDelimiter. |
63
|
|
|
$options |
64
|
|
|
; |
65
|
|
|
|
66
|
|
|
// Run the expression |
67
|
|
|
$result = preg_match($expression, $this->scalarString); |
68
|
|
|
|
69
|
|
|
// If no errors return true or false based on number of matches found. |
70
|
|
|
if ($result !== false) return $result === 1; |
71
|
|
|
|
72
|
|
|
// Otherwise throw with last known PCRE Error. |
73
|
|
|
throw new PcreException(); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Given an expression with capture groups, this will return those captures. |
78
|
|
|
* |
79
|
|
|
* Basically this is the same as `regexMatch()` but returns the array |
80
|
|
|
* of matches from `preg_match()` where as `regexMatch()` just returns |
81
|
|
|
* a boolean result. |
82
|
|
|
* |
83
|
|
|
* @param string $pattern Regex pattern to match against. |
84
|
|
|
* |
85
|
|
|
* @param string $options Matching conditions to be used. |
86
|
|
|
* |
87
|
|
|
* @return array The matches discovered by `preg_match()`. |
88
|
|
|
* |
89
|
|
|
* @throws PcreException When PCRE Error occurs. |
90
|
|
|
*/ |
91
|
|
View Code Duplication |
public function regexExtract($pattern, $options = '') |
|
|
|
|
92
|
|
|
{ |
93
|
|
|
// Define the array that will be filled by preg_match(). |
94
|
|
|
$matches = []; |
95
|
|
|
|
96
|
|
|
// Ensure the options contain the "u" modifier. |
97
|
|
|
if (!$this->newSelf($options)->contains('u')) $options .= 'u'; |
98
|
|
|
|
99
|
|
|
// Build the expression |
100
|
|
|
$expression = |
101
|
|
|
$this->regexDelimiter. |
102
|
|
|
$pattern. |
103
|
|
|
$this->regexDelimiter. |
104
|
|
|
$options |
105
|
|
|
; |
106
|
|
|
|
107
|
|
|
// Run the expression |
108
|
|
|
$result = preg_match($expression, $this->scalarString, $matches); |
109
|
|
|
|
110
|
|
|
// If no errors return the $matches array |
111
|
|
|
if ($result !== false) return $this->newSelfs($matches); |
112
|
|
|
|
113
|
|
|
// Otherwise throw with last known PCRE Error. |
114
|
|
|
throw new PcreException(); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Replaces all occurrences of $pattern in $str by $replacement. |
119
|
|
|
* |
120
|
|
|
* @param string $pattern The regular expression pattern. |
121
|
|
|
* |
122
|
|
|
* @param string $replacement The string to replace with. |
123
|
|
|
* |
124
|
|
|
* @param string $options Matching conditions to be used. |
125
|
|
|
* |
126
|
|
|
* @return static Resulting string after the replacements |
127
|
|
|
* |
128
|
|
|
* @throws PcreException When PCRE Error occurs. |
129
|
|
|
*/ |
130
|
|
View Code Duplication |
public function regexReplace($pattern, $replacement, $options = '') |
|
|
|
|
131
|
|
|
{ |
132
|
|
|
// The original regexReplace method in danielstjules/Stringy used |
133
|
|
|
// mb_ereg_replace() which supports an "r" flag, PCRE does not. |
134
|
|
|
if ($options === 'msr') $options = 'ms'; |
135
|
|
|
|
136
|
|
|
// Ensure the options contain the "u" modifier. |
137
|
|
|
if (!$this->newSelf($options)->contains('u')) $options .= 'u'; |
138
|
|
|
|
139
|
|
|
// Build the expression |
140
|
|
|
$expression = |
141
|
|
|
$this->regexDelimiter. |
142
|
|
|
$pattern. |
143
|
|
|
$this->regexDelimiter. |
144
|
|
|
$options |
145
|
|
|
; |
146
|
|
|
|
147
|
|
|
// Run the regular expression replacement |
148
|
|
|
$replaced = preg_replace($expression, $replacement, $this->scalarString); |
149
|
|
|
|
150
|
|
|
// If no errors return the replacement |
151
|
|
|
if ($replaced !== null) return $this->newSelf($replaced); |
152
|
|
|
|
153
|
|
|
// Otherwise throw with last known PCRE Error. |
154
|
|
|
throw new PcreException(); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Splits the string with the provided regular expression. |
159
|
|
|
* |
160
|
|
|
* @param string $pattern The regex with which to split the string. |
161
|
|
|
* |
162
|
|
|
* @param int|null $limit Optional maximum number of results to return. |
163
|
|
|
* |
164
|
|
|
* @param bool $quote By default this method will run the provided |
165
|
|
|
* $pattern through preg_quote(), this allows the |
166
|
|
|
* method to be used to split on simple substrings. |
167
|
|
|
* |
168
|
|
|
* @return static[] An array of Str objects. |
169
|
|
|
*/ |
170
|
|
|
public function split($pattern, $limit = null, $quote = true) |
171
|
|
|
{ |
172
|
|
|
// Not sure why you would do this but your wish is our command :) |
173
|
|
|
if ($limit === 0) return []; |
174
|
|
|
|
175
|
|
|
// UTF8::split errors when supplied an empty pattern in < PHP 5.4.13 |
176
|
|
|
// and current versions of HHVM (3.8 and below) |
177
|
|
|
if ($pattern === '') return [$this->newSelf($this->scalarString)]; |
178
|
|
|
|
179
|
|
|
// UTF8::split returns the remaining unsplit string |
180
|
|
|
// in the last index when supplying a limit. |
181
|
|
|
if ($limit > 0) |
182
|
|
|
{ |
183
|
|
|
$limit += 1; |
184
|
|
|
} |
185
|
|
|
else |
186
|
|
|
{ |
187
|
|
|
$limit = -1; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
// TODO: As per the above comments, all well and good but we are not |
|
|
|
|
191
|
|
|
// using UTF8::split here, we are using preg_split??? |
192
|
|
|
|
193
|
|
|
// Build the expression |
194
|
|
|
$expression = $this->regexDelimiter; |
195
|
|
|
|
196
|
|
|
if ($quote === true) |
197
|
|
|
{ |
198
|
|
|
$expression .= preg_quote($pattern, $this->regexDelimiter); |
199
|
|
|
} |
200
|
|
|
else |
201
|
|
|
{ |
202
|
|
|
$expression .= $pattern; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
$expression .= $this->regexDelimiter.'u'; |
206
|
|
|
|
207
|
|
|
// Split the string |
208
|
|
|
$array = preg_split($expression, $this->scalarString, $limit); |
209
|
|
|
|
210
|
|
|
// Remove any remaining unsplit string. |
211
|
|
|
if ($limit > 0 && count($array) === $limit) array_pop($array); |
212
|
|
|
|
213
|
|
|
return $this->newSelfs($array); |
214
|
|
|
} |
215
|
|
|
} |
216
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.