Skip to content

Commit 84eaebe

Browse files
committed
Merge branch 'str-between' of https://github.com/osbre/framework into osbre-str-between
2 parents 94b61bb + ec30b5e commit 84eaebe

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-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
*

src/Illuminate/Support/Stringable.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,18 @@ public function beforeLast($search)
132132
return new static(Str::beforeLast($this->value, $search));
133133
}
134134

135+
/**
136+
* Get the portion of a string between a given values.
137+
*
138+
* @param string $before
139+
* @param string $after
140+
* @return static
141+
*/
142+
public function between($before, $after)
143+
{
144+
return new static(Str::between($this->value, $before, $after));
145+
}
146+
135147
/**
136148
* Convert a value to camel case.
137149
*

tests/Support/SupportStrTest.php

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

130+
public function testStrBetween()
131+
{
132+
$this->assertSame('abc', Str::between('abc', '', 'c'));
133+
$this->assertSame('abc', Str::between('abc', 'a', ''));
134+
$this->assertSame('abc', Str::between('abc', '', ''));
135+
$this->assertSame('b', Str::between('abc', 'a', 'c'));
136+
$this->assertSame('b', Str::between('dddabc', 'a', 'c'));
137+
$this->assertSame('b', Str::between('abcddd', 'a', 'c'));
138+
$this->assertSame('b', Str::between('dddabcddd', 'a', 'c'));
139+
$this->assertSame('nn', Str::between('hannah', 'ha', 'ah'));
140+
$this->assertSame('a]ab[b', Str::between('[a]ab[b]', '[', ']'));
141+
$this->assertSame('foo', Str::between('foofoobar', 'foo', 'bar'));
142+
$this->assertSame('bar', Str::between('foobarbar', 'foo', 'bar'));
143+
}
144+
130145
public function testStrAfter()
131146
{
132147
$this->assertSame('nah', Str::after('hannah', 'han'));

0 commit comments

Comments
 (0)