Skip to content

Commit 1269551

Browse files
committed
add between() method
1 parent 1e08306 commit 1269551

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ use Flextype\Component\Strings;
5858
| <a href="#strings_segment">`Strings::segment()`</a> | Get a segment from a string based on a delimiter. Returns an empty string when the offset doesn't exist. Use a negative index to start counting from the last element. |
5959
| <a href="#strings_firstSegment">`Strings::firstSegment()`</a> | Get the first segment from a string based on a delimiter. |
6060
| <a href="#strings_lastSegment">`Strings::lastSegment()`</a> | Get the last segment from a string based on a delimiter. |
61+
| <a href="#strings_between">`Strings::between()`</a> | Get the portion of a string between two given values. |
6162
| <a href="#strings_before">`Strings::before()`</a> | Get the portion of a string before the first occurrence of a given value. |
6263
| <a href="#strings_beforeLast">`Strings::beforeLast()`</a> | Get the portion of a string before the last occurrence of a given value. |
6364
| <a href="#strings_after">`Strings::after()`</a> | Return the remainder of a string after the first occurrence of a given value. |
@@ -415,6 +416,14 @@ $string = Strings::lastSegment('SG-1 returns from an off-world mission');
415416
$string = Strings::lastSegment('SG-1 returns from an off-world mission', '-');
416417
```
417418

419+
#### <a name="strings_between"></a> Method: `Strings::between()`
420+
421+
Get the portion of a string between two given values.
422+
423+
```php
424+
$string = Strings::between('SG-1 returns from an off-world mission', 'SG-1', 'from');
425+
```
426+
418427
#### <a name="strings_before"></a> Method: `Strings::before()`
419428

420429
Get the portion of a string before the first occurrence of a given value.

src/Strings.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,22 @@ public static function lastSegment(string $string, string $delimiter = ' '): str
518518
return static::segment($string, -1, $delimiter);
519519
}
520520

521+
/**
522+
* Get the portion of a string between two given values.
523+
*
524+
* @param string $string String
525+
* @param string $from From
526+
* @param string $to To
527+
*/
528+
public static function between(string $string, string $from, string $to): string
529+
{
530+
if ($from === '' || $to === '') {
531+
return $string;
532+
}
533+
534+
return static::beforeLast(static::after($string, $from), $to);
535+
}
536+
521537
/**
522538
* Get the portion of a string before the first occurrence of a given value.
523539
*

0 commit comments

Comments
 (0)