Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create str_contains PHP function #5179

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ext/standard/basic_functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ static const zend_function_entry basic_functions[] = { /* {{{ */
PHP_FE(strtok, arginfo_strtok)
PHP_FE(strtoupper, arginfo_strtoupper)
PHP_FE(strtolower, arginfo_strtolower)
PHP_FE(str_contains, arginfo_str_contains)
PHP_FE(strpos, arginfo_strpos)
PHP_FE(stripos, arginfo_stripos)
PHP_FE(strrpos, arginfo_strrpos)
Expand Down
2 changes: 2 additions & 0 deletions ext/standard/basic_functions.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,8 @@ function stristr(string $haystack, string $needle, bool $before_needle = false):

function strstr(string $haystack, string $needle, bool $before_needle = false): string|false {}

function str_contains(string $haystack, string $needle): bool {}

function strchr(string $haystack, string $needle, bool $before_needle = false): string|false {}

function strpos(string $haystack, string $needle, int $offset = 0): int|false {}
Expand Down
5 changes: 5 additions & 0 deletions ext/standard/basic_functions_arginfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -899,6 +899,11 @@ ZEND_END_ARG_INFO()

#define arginfo_strstr arginfo_stristr

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_str_contains, 0, 2, _IS_BOOL, 0)
ZEND_ARG_TYPE_INFO(0, haystack, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, needle, IS_STRING, 0)
ZEND_END_ARG_INFO()
nikic marked this conversation as resolved.
Show resolved Hide resolved

#define arginfo_strchr arginfo_stristr

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_strpos, 0, 2, MAY_BE_LONG|MAY_BE_FALSE)
Expand Down
1 change: 1 addition & 0 deletions ext/standard/php_string.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ PHP_FUNCTION(basename);
PHP_FUNCTION(dirname);
PHP_FUNCTION(pathinfo);
PHP_FUNCTION(strstr);
PHP_FUNCTION(str_contains);
PHP_FUNCTION(strpos);
PHP_FUNCTION(stripos);
PHP_FUNCTION(strrpos);
Expand Down
15 changes: 15 additions & 0 deletions ext/standard/string.c
Original file line number Diff line number Diff line change
Expand Up @@ -1851,6 +1851,21 @@ PHP_FUNCTION(strstr)
}
/* }}} */

/* {{{ proto bool str_contains(string haystack, string needle)
Checks if a string contains another */
PHP_FUNCTION(str_contains)
{
zend_string *haystack, *needle;

ZEND_PARSE_PARAMETERS_START(2, 2)
Z_PARAM_STR(haystack)
Z_PARAM_STR(needle)
ZEND_PARSE_PARAMETERS_END();

RETURN_BOOL(php_memnstr(ZSTR_VAL(haystack), ZSTR_VAL(needle), ZSTR_LEN(needle), ZSTR_VAL(haystack) + ZSTR_LEN(haystack)));
}
/* }}} */

/* {{{ proto string strchr(string haystack, string needle)
An alias for strstr */
/* }}} */
Expand Down
34 changes: 34 additions & 0 deletions ext/standard/tests/strings/str_contains.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
--TEST--
Test str_contains() function
--FILE--
<?php
/* Prototype: str_contains ( string $haystack , string $needle ) : bool
Description: Check if a string contains another string
Source code: ext/standard/string.c
*/
var_dump(str_contains("test string", "test"));
var_dump(str_contains("test string", "string"));
var_dump(str_contains("test string", "strin"));
var_dump(str_contains("test string", "t s"));
var_dump(str_contains("test string", "g"));
var_dump(str_contains("te".chr(0)."st", chr(0)));
var_dump(str_contains("tEst", "test"));
var_dump(str_contains("teSt", "test"));
var_dump(str_contains("", ""));
var_dump(str_contains("a", ""));
var_dump(str_contains("", "a"));
var_dump(str_contains("\\\\a", "\\a"));
?>
--EXPECT--
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(false)
bool(false)
bool(true)
bool(true)
bool(false)
bool(true)