Skip to content

Commit 8f1b8e9

Browse files
feature #229 [PHP 8.0] add str_contains function (IonBazan)
This PR was merged into the 1.15-dev branch. Discussion ---------- [PHP 8.0] add `str_contains` function Waiting for https://wiki.php.net/rfc/str_contains RFC. Commits ------- e80a892 add `str_contains` function
2 parents 8ba9f43 + e80a892 commit 8f1b8e9

File tree

5 files changed

+29
-0
lines changed

5 files changed

+29
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ Polyfills are provided for:
4545
introduced in PHP 7.4;
4646
- the `fdiv` function introduced in PHP 8.0;
4747
- the `preg_last_error_msg` function introduced in PHP 8.0;
48+
- the `str_contains` function introduced in PHP 8.0;
4849
- the `ValueError` class introduced in PHP 8.0;
4950
- the `FILTER_VALIDATE_BOOL` constant introduced in PHP 8.0;
5051

src/Php80/Php80.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,9 @@ public static function preg_last_error_msg(): string
4545
return 'Unknown error';
4646
}
4747
}
48+
49+
public static function str_contains(string $haystack, string $needle): bool
50+
{
51+
return '' === $needle || false !== strpos($haystack, $needle);
52+
}
4853
}

src/Php80/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ This component provides features added to PHP 8.0 core:
88
- `ValueError` class
99
- `FILTER_VALIDATE_BOOL` constant
1010
- [`preg_last_error_msg`](https://php.net/preg_last_error_msg)
11+
- [`str_contains`](https://php.net/str_contains)
1112

1213
More information can be found in the
1314
[main Polyfill README](https://github.com/symfony/polyfill/blob/master/README.md).

src/Php80/bootstrap.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ function fdiv(float $dividend, float $divisor): float { return p\Php80::fdiv($di
2020
function preg_last_error_msg(): string { return p\Php80::preg_last_error_msg(); }
2121
}
2222

23+
if (!function_exists('str_contains')) {
24+
function str_contains(string $haystack, string $needle): bool { return p\Php80::str_contains($haystack, $needle); }
25+
}
26+
2327
if (!defined('FILTER_VALIDATE_BOOL') && defined('FILTER_VALIDATE_BOOLEAN')) {
2428
define('FILTER_VALIDATE_BOOL', FILTER_VALIDATE_BOOLEAN);
2529
}

tests/Php80/Php80Test.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,24 @@ public function testPregMalformedUtf8Offset()
8787
);
8888
}
8989

90+
/**
91+
* @covers \Symfony\Polyfill\Php80\Php80::str_contains
92+
*/
93+
public function testStrContains()
94+
{
95+
$this->assertTrue(str_contains('abc', ''));
96+
$this->assertTrue(str_contains('abc', 'a'));
97+
$this->assertTrue(str_contains('abc', 'bc'));
98+
$this->assertTrue(str_contains('abc', 'abc'));
99+
$this->assertTrue(str_contains('한국어', ''));
100+
$this->assertTrue(str_contains('한국어', ''));
101+
$this->assertTrue(str_contains('', ''));
102+
$this->assertFalse(str_contains('abc', 'd'));
103+
$this->assertFalse(str_contains('abc', 'abcd'));
104+
$this->assertFalse(str_contains('DÉJÀ', 'à'));
105+
$this->assertFalse(str_contains('a', 'à'));
106+
}
107+
90108
public function fdivProvider()
91109
{
92110
return array(

0 commit comments

Comments
 (0)