|
4 | 4 |
|
5 | 5 | use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled;
|
6 | 6 | use PhpOffice\PhpSpreadsheet\Calculation\Exception as CalcExp;
|
| 7 | +use PhpOffice\PhpSpreadsheet\Calculation\Functions; |
| 8 | +use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError; |
| 9 | +use PhpOffice\PhpSpreadsheet\Shared\StringHelper; |
7 | 10 |
|
8 | 11 | class Extract
|
9 | 12 | {
|
@@ -95,4 +98,157 @@ public static function right($value, $chars = 1)
|
95 | 98 |
|
96 | 99 | return mb_substr($value ?? '', mb_strlen($value ?? '', 'UTF-8') - $chars, $chars, 'UTF-8');
|
97 | 100 | }
|
| 101 | + |
| 102 | + /** |
| 103 | + * TEXTBEFORE. |
| 104 | + * |
| 105 | + * @param mixed $text the text that you're searching |
| 106 | + * Or can be an array of values |
| 107 | + * @param ?string $delimiter the text that marks the point before which you want to extract |
| 108 | + * @param mixed $instance The instance of the delimiter after which you want to extract the text. |
| 109 | + * By default, this is the first instance (1). |
| 110 | + * A negative value means start searching from the end of the text string. |
| 111 | + * Or can be an array of values |
| 112 | + * @param mixed $matchMode Determines whether the match is case-sensitive or not. |
| 113 | + * 0 - Case-sensitive |
| 114 | + * 1 - Case-insensitive |
| 115 | + * Or can be an array of values |
| 116 | + * @param mixed $matchEnd Treats the end of text as a delimiter. |
| 117 | + * 0 - Don't match the delimiter against the end of the text. |
| 118 | + * 1 - Match the delimiter against the end of the text. |
| 119 | + * Or can be an array of values |
| 120 | + * @param mixed $ifNotFound value to return if no match is found |
| 121 | + * The default is a #N/A Error |
| 122 | + * Or can be an array of values |
| 123 | + * |
| 124 | + * @return mixed|mixed[] the string extracted from text before the delimiter; or the $ifNotFound value |
| 125 | + * If an array of values is passed for any of the arguments, then the returned result |
| 126 | + * will also be an array with matching dimensions |
| 127 | + */ |
| 128 | + public static function before($text, $delimiter, $instance = 1, $matchMode = 0, $matchEnd = 0, $ifNotFound = '#N/A') |
| 129 | + { |
| 130 | + if (is_array($text) || is_array($instance) || is_array($matchMode) || is_array($matchEnd) || is_array($ifNotFound)) { |
| 131 | + return self::evaluateArrayArgumentsIgnore([self::class, __FUNCTION__], 1, $text, $delimiter, $instance, $matchMode, $matchEnd, $ifNotFound); |
| 132 | + } |
| 133 | + |
| 134 | + $text = Helpers::extractString($text ?? ''); |
| 135 | + $delimiter = Helpers::extractString(Functions::flattenSingleValue($delimiter ?? '')); |
| 136 | + $instance = (int) $instance; |
| 137 | + $matchMode = (int) $matchMode; |
| 138 | + $matchEnd = (int) $matchEnd; |
| 139 | + |
| 140 | + $split = self::validateTextBeforeAfter($text, $delimiter, $instance, $matchMode, $matchEnd, $ifNotFound); |
| 141 | + if (is_array($split) === false) { |
| 142 | + return $split; |
| 143 | + } |
| 144 | + if ($delimiter === '') { |
| 145 | + return ($instance > 0) ? '' : $text; |
| 146 | + } |
| 147 | + |
| 148 | + // Adjustment for a match as the first element of the split |
| 149 | + $flags = self::matchFlags($matchMode); |
| 150 | + $adjust = preg_match('/^' . preg_quote($delimiter) . "\$/{$flags}", $split[0]); |
| 151 | + $oddReverseAdjustment = count($split) % 2; |
| 152 | + |
| 153 | + $split = ($instance < 0) |
| 154 | + ? array_slice($split, 0, max(count($split) - (abs($instance) * 2 - 1) - $adjust - $oddReverseAdjustment, 0)) |
| 155 | + : array_slice($split, 0, $instance * 2 - 1 - $adjust); |
| 156 | + |
| 157 | + return implode('', $split); |
| 158 | + } |
| 159 | + |
| 160 | + /** |
| 161 | + * TEXTAFTER. |
| 162 | + * |
| 163 | + * @param mixed $text the text that you're searching |
| 164 | + * @param ?string $delimiter the text that marks the point before which you want to extract |
| 165 | + * @param mixed $instance The instance of the delimiter after which you want to extract the text. |
| 166 | + * By default, this is the first instance (1). |
| 167 | + * A negative value means start searching from the end of the text string. |
| 168 | + * Or can be an array of values |
| 169 | + * @param mixed $matchMode Determines whether the match is case-sensitive or not. |
| 170 | + * 0 - Case-sensitive |
| 171 | + * 1 - Case-insensitive |
| 172 | + * Or can be an array of values |
| 173 | + * @param mixed $matchEnd Treats the end of text as a delimiter. |
| 174 | + * 0 - Don't match the delimiter against the end of the text. |
| 175 | + * 1 - Match the delimiter against the end of the text. |
| 176 | + * Or can be an array of values |
| 177 | + * @param mixed $ifNotFound value to return if no match is found |
| 178 | + * The default is a #N/A Error |
| 179 | + * Or can be an array of values |
| 180 | + * |
| 181 | + * @return mixed|mixed[] the string extracted from text before the delimiter; or the $ifNotFound value |
| 182 | + * If an array of values is passed for any of the arguments, then the returned result |
| 183 | + * will also be an array with matching dimensions |
| 184 | + */ |
| 185 | + public static function after($text, $delimiter, $instance = 1, $matchMode = 0, $matchEnd = 0, $ifNotFound = '#N/A') |
| 186 | + { |
| 187 | + if (is_array($text) || is_array($instance) || is_array($matchMode) || is_array($matchEnd) || is_array($ifNotFound)) { |
| 188 | + return self::evaluateArrayArgumentsIgnore([self::class, __FUNCTION__], 1, $text, $delimiter, $instance, $matchMode, $matchEnd, $ifNotFound); |
| 189 | + } |
| 190 | + |
| 191 | + $text = Helpers::extractString($text ?? ''); |
| 192 | + $delimiter = Helpers::extractString(Functions::flattenSingleValue($delimiter ?? '')); |
| 193 | + $instance = (int) $instance; |
| 194 | + $matchMode = (int) $matchMode; |
| 195 | + $matchEnd = (int) $matchEnd; |
| 196 | + |
| 197 | + $split = self::validateTextBeforeAfter($text, $delimiter, $instance, $matchMode, $matchEnd, $ifNotFound); |
| 198 | + if (is_array($split) === false) { |
| 199 | + return $split; |
| 200 | + } |
| 201 | + if ($delimiter === '') { |
| 202 | + return ($instance < 0) ? '' : $text; |
| 203 | + } |
| 204 | + |
| 205 | + // Adjustment for a match as the first element of the split |
| 206 | + $flags = self::matchFlags($matchMode); |
| 207 | + $adjust = preg_match('/^' . preg_quote($delimiter) . "\$/{$flags}", $split[0]); |
| 208 | + $oddReverseAdjustment = count($split) % 2; |
| 209 | + |
| 210 | + $split = ($instance < 0) |
| 211 | + ? array_slice($split, count($split) - (abs($instance + 1) * 2) - $adjust - $oddReverseAdjustment) |
| 212 | + : array_slice($split, $instance * 2 - $adjust); |
| 213 | + |
| 214 | + return implode('', $split); |
| 215 | + } |
| 216 | + |
| 217 | + /** |
| 218 | + * @param int $matchMode |
| 219 | + * @param int $matchEnd |
| 220 | + * @param mixed $ifNotFound |
| 221 | + * |
| 222 | + * @return string|string[] |
| 223 | + */ |
| 224 | + private static function validateTextBeforeAfter(string $text, string $delimiter, int $instance, $matchMode, $matchEnd, $ifNotFound) |
| 225 | + { |
| 226 | + $flags = self::matchFlags($matchMode); |
| 227 | + |
| 228 | + if (preg_match('/' . preg_quote($delimiter) . "/{$flags}", $text) === 0 && $matchEnd === 0) { |
| 229 | + return $ifNotFound; |
| 230 | + } |
| 231 | + |
| 232 | + $split = preg_split('/(' . preg_quote($delimiter) . ")/{$flags}", $text, 0, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE); |
| 233 | + if ($split === false) { |
| 234 | + return ExcelError::NA(); |
| 235 | + } |
| 236 | + |
| 237 | + if ($instance === 0 || abs($instance) > StringHelper::countCharacters($text)) { |
| 238 | + return ExcelError::VALUE(); |
| 239 | + } |
| 240 | + |
| 241 | + if ($matchEnd === 0 && (abs($instance) > floor(count($split) / 2))) { |
| 242 | + return ExcelError::NA(); |
| 243 | + } elseif ($matchEnd !== 0 && (abs($instance) - 1 > ceil(count($split) / 2))) { |
| 244 | + return ExcelError::NA(); |
| 245 | + } |
| 246 | + |
| 247 | + return $split; |
| 248 | + } |
| 249 | + |
| 250 | + private static function matchFlags(int $matchMode): string |
| 251 | + { |
| 252 | + return ($matchMode === 0) ? 'mu' : 'miu'; |
| 253 | + } |
98 | 254 | }
|
0 commit comments