Skip to content

Commit c354a69

Browse files
committed
Add Str::between method
1 parent 9495b28 commit c354a69

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

src/Illuminate/Support/Str.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,25 @@ public static function beforeLast($subject, $search)
132132
return static::substr($subject, 0, $pos);
133133
}
134134

135+
/**
136+
* Get the portion of a string between a given values.
137+
*
138+
* @param string $subject
139+
* @param string $before
140+
* @param string $after
141+
* @return string
142+
*/
143+
public static function between($subject, $before, $after)
144+
{
145+
if ($before === '' || $after === '') {
146+
return $subject;
147+
}
148+
149+
$rightCropped = static::after($subject, $before);
150+
151+
return static::beforeLast($rightCropped, $after);
152+
}
153+
135154
/**
136155
* Convert a value to camel case.
137156
*

tests/Support/SupportStrTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,13 @@ public function testStrBeforeLast()
127127
$this->assertSame('yv2et', Str::beforeLast('yv2et2te', 2));
128128
}
129129

130+
public function testStrBetween()
131+
{
132+
$this->assertSame('nn', Str::between('hannah', 'ha', 'ah'));
133+
$this->assertSame('foo', Str::between('foofoobar', 'foo', 'bar'));
134+
$this->assertSame('bar', Str::between('foobarbar', 'foo', 'bar'));
135+
}
136+
130137
public function testStrAfter()
131138
{
132139
$this->assertSame('nah', Str::after('hannah', 'han'));

0 commit comments

Comments
 (0)