Skip to content

Commit 1668ad7

Browse files
philipptanikic
authored andcommitted
Add str_contains() function
RFC: https://wiki.php.net/rfc/str_contains Closes GH-5179.
1 parent 934e60b commit 1668ad7

File tree

7 files changed

+62
-1
lines changed

7 files changed

+62
-1
lines changed

UPGRADING

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,10 @@ PHP 8.0 UPGRADE NOTES
481481
PR: https://github.com/php/php-src/pull/4797
482482

483483
- Standard:
484-
. Added fdiv() method, which performs a floating-point division under
484+
. Added str_contains($haystack, $needle) function, which checks whether
485+
$haystack contains $needle as a sub-string. It is equivalent to writing
486+
strpos($haystack, $needle) !== false.
487+
. Added fdiv() function, which performs a floating-point division under
485488
IEEE 754 semantics. Division by zero is considered well-defined and
486489
will return one of Inf, -Inf or NaN.
487490

ext/standard/basic_functions.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ static const zend_function_entry basic_functions[] = { /* {{{ */
173173
PHP_FE(strtok, arginfo_strtok)
174174
PHP_FE(strtoupper, arginfo_strtoupper)
175175
PHP_FE(strtolower, arginfo_strtolower)
176+
PHP_FE(str_contains, arginfo_str_contains)
176177
PHP_FE(strpos, arginfo_strpos)
177178
PHP_FE(stripos, arginfo_stripos)
178179
PHP_FE(strrpos, arginfo_strrpos)

ext/standard/basic_functions.stub.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,8 @@ function strripos(string $haystack, string $needle, int $offset = 0): int|false
586586

587587
function strrchr(string $haystack, string $needle): string|false {}
588588

589+
function str_contains(string $haystack, string $needle): bool {}
590+
589591
function chunk_split(string $str, int $chunklen = 76, string $ending = "\r\n"): string {}
590592

591593
function substr(string $str, int $start, ?int $length = null): string|false {}

ext/standard/basic_functions_arginfo.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -918,6 +918,11 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_strrchr, 0, 2, MAY_BE_STRING|MAY
918918
ZEND_ARG_TYPE_INFO(0, needle, IS_STRING, 0)
919919
ZEND_END_ARG_INFO()
920920

921+
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_str_contains, 0, 2, _IS_BOOL, 0)
922+
ZEND_ARG_TYPE_INFO(0, haystack, IS_STRING, 0)
923+
ZEND_ARG_TYPE_INFO(0, needle, IS_STRING, 0)
924+
ZEND_END_ARG_INFO()
925+
921926
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_chunk_split, 0, 1, IS_STRING, 0)
922927
ZEND_ARG_TYPE_INFO(0, str, IS_STRING, 0)
923928
ZEND_ARG_TYPE_INFO(0, chunklen, IS_LONG, 0)

ext/standard/php_string.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ PHP_FUNCTION(basename);
3939
PHP_FUNCTION(dirname);
4040
PHP_FUNCTION(pathinfo);
4141
PHP_FUNCTION(strstr);
42+
PHP_FUNCTION(str_contains);
4243
PHP_FUNCTION(strpos);
4344
PHP_FUNCTION(stripos);
4445
PHP_FUNCTION(strrpos);

ext/standard/string.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1851,6 +1851,21 @@ PHP_FUNCTION(strstr)
18511851
}
18521852
/* }}} */
18531853

1854+
/* {{{ proto bool str_contains(string haystack, string needle)
1855+
Checks if a string contains another */
1856+
PHP_FUNCTION(str_contains)
1857+
{
1858+
zend_string *haystack, *needle;
1859+
1860+
ZEND_PARSE_PARAMETERS_START(2, 2)
1861+
Z_PARAM_STR(haystack)
1862+
Z_PARAM_STR(needle)
1863+
ZEND_PARSE_PARAMETERS_END();
1864+
1865+
RETURN_BOOL(php_memnstr(ZSTR_VAL(haystack), ZSTR_VAL(needle), ZSTR_LEN(needle), ZSTR_VAL(haystack) + ZSTR_LEN(haystack)));
1866+
}
1867+
/* }}} */
1868+
18541869
/* {{{ proto string strchr(string haystack, string needle)
18551870
An alias for strstr */
18561871
/* }}} */
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
--TEST--
2+
Test str_contains() function
3+
--FILE--
4+
<?php
5+
/* Prototype: str_contains ( string $haystack , string $needle ) : bool
6+
Description: Check if a string contains another string
7+
Source code: ext/standard/string.c
8+
*/
9+
var_dump(str_contains("test string", "test"));
10+
var_dump(str_contains("test string", "string"));
11+
var_dump(str_contains("test string", "strin"));
12+
var_dump(str_contains("test string", "t s"));
13+
var_dump(str_contains("test string", "g"));
14+
var_dump(str_contains("te".chr(0)."st", chr(0)));
15+
var_dump(str_contains("tEst", "test"));
16+
var_dump(str_contains("teSt", "test"));
17+
var_dump(str_contains("", ""));
18+
var_dump(str_contains("a", ""));
19+
var_dump(str_contains("", "a"));
20+
var_dump(str_contains("\\\\a", "\\a"));
21+
?>
22+
--EXPECT--
23+
bool(true)
24+
bool(true)
25+
bool(true)
26+
bool(true)
27+
bool(true)
28+
bool(true)
29+
bool(false)
30+
bool(false)
31+
bool(true)
32+
bool(true)
33+
bool(false)
34+
bool(true)

0 commit comments

Comments
 (0)