Skip to content

Commit f0ef58d

Browse files
[10.x] introduce Str::substrPos (#48421)
* implement Str::substrPos helper method (fluent and static) * apply ci fixes for Str::substrPos() * additional tests for Str::substrPos() * formatting * formatting --------- Co-authored-by: Taylor Otwell <taylor@laravel.com>
1 parent d95209b commit f0ef58d

File tree

4 files changed

+61
-0
lines changed

4 files changed

+61
-0
lines changed

src/Illuminate/Support/Str.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -861,6 +861,20 @@ public static function password($length = 32, $letters = true, $numbers = true,
861861
->implode('');
862862
}
863863

864+
/**
865+
* Find the multi-byte safe position of the first occurrence of a given substring in a string.
866+
*
867+
* @param string $haystack
868+
* @param string $needle
869+
* @param int $offset
870+
* @param string|null $encoding
871+
* @return int|false
872+
*/
873+
public static function position($haystack, $needle, $offset = 0, $encoding = null)
874+
{
875+
return mb_strpos($haystack, (string) $needle, $offset, $encoding);
876+
}
877+
864878
/**
865879
* Generate a more truly "random" alpha-numeric string.
866880
*

src/Illuminate/Support/Stringable.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,19 @@ public function pluralStudly($count = 2)
575575
return new static(Str::pluralStudly($this->value, $count));
576576
}
577577

578+
/**
579+
* Find the multi-byte safe position of the first occurrence of the given substring.
580+
*
581+
* @param string $needle
582+
* @param int $offset
583+
* @param string|null $encoding
584+
* @return int|false
585+
*/
586+
public function position($needle, $offset = 0, $encoding = null)
587+
{
588+
return Str::position($this->value, $needle, $offset, $encoding);
589+
}
590+
578591
/**
579592
* Prepend the given values to the string.
580593
*

tests/Support/SupportStrTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -823,6 +823,23 @@ public function testSubstrCount()
823823
$this->assertSame(1, Str::substrCount('laravelPHPFramework', 'a', -10, -3));
824824
}
825825

826+
public function testPosition()
827+
{
828+
$this->assertSame(7, Str::position('Hello, World!', 'W'));
829+
$this->assertSame(10, Str::position('This is a test string.', 'test'));
830+
$this->assertSame(23, Str::position('This is a test string, test again.', 'test', 15));
831+
$this->assertSame(0, Str::position('Hello, World!', 'Hello'));
832+
$this->assertSame(7, Str::position('Hello, World!', 'World!'));
833+
$this->assertSame(10, Str::position('This is a tEsT string.', 'tEsT', 0, 'UTF-8'));
834+
$this->assertSame(7, Str::position('Hello, World!', 'W', -6));
835+
$this->assertSame(18, Str::position('Äpfel, Birnen und Kirschen', 'Kirschen', -10, 'UTF-8'));
836+
$this->assertSame(9, Str::position('@%€/=!"][$', '$', 0, 'UTF-8'));
837+
$this->assertFalse(Str::position('Hello, World!', 'w', 0, 'UTF-8'));
838+
$this->assertFalse(Str::position('Hello, World!', 'X', 0, 'UTF-8'));
839+
$this->assertFalse(Str::position('', 'test'));
840+
$this->assertFalse(Str::position('Hello, World!', 'X'));
841+
}
842+
826843
public function testSubstrReplace()
827844
{
828845
$this->assertSame('12:00', Str::substrReplace('1200', ':', 2, 0));

tests/Support/SupportStringableTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1018,6 +1018,23 @@ public function testSubstrCount()
10181018
$this->assertSame(1, $this->stringable('laravelPHPFramework')->substrCount('a', -10, -3));
10191019
}
10201020

1021+
public function testSubstrPos()
1022+
{
1023+
$this->assertSame(7, $this->stringable('Hello, World!')->position('W'));
1024+
$this->assertSame(10, $this->stringable('This is a test string.')->position('test'));
1025+
$this->assertSame(23, $this->stringable('This is a test string, test again.')->position('test', 15));
1026+
$this->assertSame(0, $this->stringable('Hello, World!')->position('Hello'));
1027+
$this->assertSame(7, $this->stringable('Hello, World!')->position('World!'));
1028+
$this->assertSame(10, $this->stringable('This is a tEsT string.')->position('tEsT', 0, 'UTF-8'));
1029+
$this->assertSame(7, $this->stringable('Hello, World!')->position('W', -6));
1030+
$this->assertSame(18, $this->stringable('Äpfel, Birnen und Kirschen')->position('Kirschen', -10, 'UTF-8'));
1031+
$this->assertSame(9, $this->stringable('@%€/=!"][$')->position('$', 0, 'UTF-8'));
1032+
$this->assertFalse($this->stringable('Hello, World!')->position('w', 0, 'UTF-8'));
1033+
$this->assertFalse($this->stringable('Hello, World!')->position('X', 0, 'UTF-8'));
1034+
$this->assertFalse($this->stringable('')->position('test'));
1035+
$this->assertFalse($this->stringable('Hello, World!')->position('X'));
1036+
}
1037+
10211038
public function testSubstrReplace()
10221039
{
10231040
$this->assertSame('12:00', (string) $this->stringable('1200')->substrReplace(':', 2, 0));

0 commit comments

Comments
 (0)