Extract.php
11.9 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
<?php
namespace PhpOffice\PhpSpreadsheet\Calculation\TextData;
use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled;
use PhpOffice\PhpSpreadsheet\Calculation\Exception as CalcExp;
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError;
use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
class Extract
{
use ArrayEnabled;
/**
* LEFT.
*
* @param mixed $value String value from which to extract characters
* Or can be an array of values
* @param mixed $chars The number of characters to extract (as an integer)
* Or can be an array of values
*
* @return array|string The joined string
* If an array of values is passed for the $value or $chars arguments, then the returned result
* will also be an array with matching dimensions
*/
public static function left($value, $chars = 1)
{
if (is_array($value) || is_array($chars)) {
return self::evaluateArrayArguments([self::class, __FUNCTION__], $value, $chars);
}
try {
$value = Helpers::extractString($value);
$chars = Helpers::extractInt($chars, 0, 1);
} catch (CalcExp $e) {
return $e->getMessage();
}
return mb_substr($value ?? '', 0, $chars, 'UTF-8');
}
/**
* MID.
*
* @param mixed $value String value from which to extract characters
* Or can be an array of values
* @param mixed $start Integer offset of the first character that we want to extract
* Or can be an array of values
* @param mixed $chars The number of characters to extract (as an integer)
* Or can be an array of values
*
* @return array|string The joined string
* If an array of values is passed for the $value, $start or $chars arguments, then the returned result
* will also be an array with matching dimensions
*/
public static function mid($value, $start, $chars)
{
if (is_array($value) || is_array($start) || is_array($chars)) {
return self::evaluateArrayArguments([self::class, __FUNCTION__], $value, $start, $chars);
}
try {
$value = Helpers::extractString($value);
$start = Helpers::extractInt($start, 1);
$chars = Helpers::extractInt($chars, 0);
} catch (CalcExp $e) {
return $e->getMessage();
}
return mb_substr($value ?? '', --$start, $chars, 'UTF-8');
}
/**
* RIGHT.
*
* @param mixed $value String value from which to extract characters
* Or can be an array of values
* @param mixed $chars The number of characters to extract (as an integer)
* Or can be an array of values
*
* @return array|string The joined string
* If an array of values is passed for the $value or $chars arguments, then the returned result
* will also be an array with matching dimensions
*/
public static function right($value, $chars = 1)
{
if (is_array($value) || is_array($chars)) {
return self::evaluateArrayArguments([self::class, __FUNCTION__], $value, $chars);
}
try {
$value = Helpers::extractString($value);
$chars = Helpers::extractInt($chars, 0, 1);
} catch (CalcExp $e) {
return $e->getMessage();
}
return mb_substr($value ?? '', mb_strlen($value ?? '', 'UTF-8') - $chars, $chars, 'UTF-8');
}
/**
* TEXTBEFORE.
*
* @param mixed $text the text that you're searching
* Or can be an array of values
* @param null|array|string $delimiter the text that marks the point before which you want to extract
* Multiple delimiters can be passed as an array of string values
* @param mixed $instance The instance of the delimiter after which you want to extract the text.
* By default, this is the first instance (1).
* A negative value means start searching from the end of the text string.
* Or can be an array of values
* @param mixed $matchMode Determines whether the match is case-sensitive or not.
* 0 - Case-sensitive
* 1 - Case-insensitive
* Or can be an array of values
* @param mixed $matchEnd Treats the end of text as a delimiter.
* 0 - Don't match the delimiter against the end of the text.
* 1 - Match the delimiter against the end of the text.
* Or can be an array of values
* @param mixed $ifNotFound value to return if no match is found
* The default is a #N/A Error
* Or can be an array of values
*
* @return mixed|mixed[] the string extracted from text before the delimiter; or the $ifNotFound value
* If an array of values is passed for any of the arguments, then the returned result
* will also be an array with matching dimensions
*/
public static function before($text, $delimiter, $instance = 1, $matchMode = 0, $matchEnd = 0, $ifNotFound = '#N/A')
{
if (is_array($text) || is_array($instance) || is_array($matchMode) || is_array($matchEnd) || is_array($ifNotFound)) {
return self::evaluateArrayArgumentsIgnore([self::class, __FUNCTION__], 1, $text, $delimiter, $instance, $matchMode, $matchEnd, $ifNotFound);
}
$text = Helpers::extractString($text ?? '');
$instance = (int) $instance;
$matchMode = (int) $matchMode;
$matchEnd = (int) $matchEnd;
$split = self::validateTextBeforeAfter($text, $delimiter, $instance, $matchMode, $matchEnd, $ifNotFound);
if (is_array($split) === false) {
return $split;
}
if (Helpers::extractString(Functions::flattenSingleValue($delimiter ?? '')) === '') {
return ($instance > 0) ? '' : $text;
}
// Adjustment for a match as the first element of the split
$flags = self::matchFlags($matchMode);
$delimiter = self::buildDelimiter($delimiter);
$adjust = preg_match('/^' . $delimiter . "\$/{$flags}", $split[0]);
$oddReverseAdjustment = count($split) % 2;
$split = ($instance < 0)
? array_slice($split, 0, max(count($split) - (abs($instance) * 2 - 1) - $adjust - $oddReverseAdjustment, 0))
: array_slice($split, 0, $instance * 2 - 1 - $adjust);
return implode('', $split);
}
/**
* TEXTAFTER.
*
* @param mixed $text the text that you're searching
* @param null|array|string $delimiter the text that marks the point before which you want to extract
* Multiple delimiters can be passed as an array of string values
* @param mixed $instance The instance of the delimiter after which you want to extract the text.
* By default, this is the first instance (1).
* A negative value means start searching from the end of the text string.
* Or can be an array of values
* @param mixed $matchMode Determines whether the match is case-sensitive or not.
* 0 - Case-sensitive
* 1 - Case-insensitive
* Or can be an array of values
* @param mixed $matchEnd Treats the end of text as a delimiter.
* 0 - Don't match the delimiter against the end of the text.
* 1 - Match the delimiter against the end of the text.
* Or can be an array of values
* @param mixed $ifNotFound value to return if no match is found
* The default is a #N/A Error
* Or can be an array of values
*
* @return mixed|mixed[] the string extracted from text before the delimiter; or the $ifNotFound value
* If an array of values is passed for any of the arguments, then the returned result
* will also be an array with matching dimensions
*/
public static function after($text, $delimiter, $instance = 1, $matchMode = 0, $matchEnd = 0, $ifNotFound = '#N/A')
{
if (is_array($text) || is_array($instance) || is_array($matchMode) || is_array($matchEnd) || is_array($ifNotFound)) {
return self::evaluateArrayArgumentsIgnore([self::class, __FUNCTION__], 1, $text, $delimiter, $instance, $matchMode, $matchEnd, $ifNotFound);
}
$text = Helpers::extractString($text ?? '');
$instance = (int) $instance;
$matchMode = (int) $matchMode;
$matchEnd = (int) $matchEnd;
$split = self::validateTextBeforeAfter($text, $delimiter, $instance, $matchMode, $matchEnd, $ifNotFound);
if (is_array($split) === false) {
return $split;
}
if (Helpers::extractString(Functions::flattenSingleValue($delimiter ?? '')) === '') {
return ($instance < 0) ? '' : $text;
}
// Adjustment for a match as the first element of the split
$flags = self::matchFlags($matchMode);
$delimiter = self::buildDelimiter($delimiter);
$adjust = preg_match('/^' . $delimiter . "\$/{$flags}", $split[0]);
$oddReverseAdjustment = count($split) % 2;
$split = ($instance < 0)
? array_slice($split, count($split) - (abs($instance + 1) * 2) - $adjust - $oddReverseAdjustment)
: array_slice($split, $instance * 2 - $adjust);
return implode('', $split);
}
/**
* @param null|array|string $delimiter
* @param int $matchMode
* @param int $matchEnd
* @param mixed $ifNotFound
*
* @return string|string[]
*/
private static function validateTextBeforeAfter(string $text, $delimiter, int $instance, $matchMode, $matchEnd, $ifNotFound)
{
$flags = self::matchFlags($matchMode);
$delimiter = self::buildDelimiter($delimiter);
if (preg_match('/' . $delimiter . "/{$flags}", $text) === 0 && $matchEnd === 0) {
return $ifNotFound;
}
$split = preg_split('/' . $delimiter . "/{$flags}", $text, 0, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
if ($split === false) {
return ExcelError::NA();
}
if ($instance === 0 || abs($instance) > StringHelper::countCharacters($text)) {
return ExcelError::VALUE();
}
if ($matchEnd === 0 && (abs($instance) > floor(count($split) / 2))) {
return ExcelError::NA();
} elseif ($matchEnd !== 0 && (abs($instance) - 1 > ceil(count($split) / 2))) {
return ExcelError::NA();
}
return $split;
}
/**
* @param null|array|string $delimiter the text that marks the point before which you want to extract
* Multiple delimiters can be passed as an array of string values
*/
private static function buildDelimiter($delimiter): string
{
if (is_array($delimiter)) {
$delimiter = Functions::flattenArray($delimiter);
$quotedDelimiters = array_map(
function ($delimiter) {
return preg_quote($delimiter ?? '');
},
$delimiter
);
$delimiters = implode('|', $quotedDelimiters);
return '(' . $delimiters . ')';
}
return '(' . preg_quote($delimiter ?? '') . ')';
}
private static function matchFlags(int $matchMode): string
{
return ($matchMode === 0) ? 'mu' : 'miu';
}
}