Skip to content

Create str_icontains PHP function #18705

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions ext/standard/basic_functions.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -2439,6 +2439,11 @@ function strrchr(string $haystack, string $needle, bool $before_needle = false):
*/
function str_contains(string $haystack, string $needle): bool {}

/**
* @compile-time-eval
*/
function str_icontains(string $haystack, string $needle): bool {}

/**
* @compile-time-eval
* @frameless-function {"arity": 2}
Expand Down
6 changes: 5 additions & 1 deletion ext/standard/basic_functions_arginfo.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions ext/standard/string.c
Original file line number Diff line number Diff line change
Expand Up @@ -1836,6 +1836,20 @@ ZEND_FRAMELESS_FUNCTION(str_contains, 2)
Z_FLF_PARAM_FREE_STR(2, needle_tmp);
}

/* {{{ Checks if a string contains another, case insensitive */
PHP_FUNCTION(str_icontains)
{
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_memnistr(ZSTR_VAL(haystack), ZSTR_VAL(needle), ZSTR_LEN(needle), ZSTR_VAL(haystack) + ZSTR_LEN(haystack)));
}
/* }}} */

/* {{{ Checks if haystack starts with needle */
PHP_FUNCTION(str_starts_with)
{
Expand Down
36 changes: 36 additions & 0 deletions ext/standard/tests/strings/str_icontains.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
--TEST--
Test str_icontains() function
--FILE--
<?php
var_dump(str_icontains("test string", "test"));
var_dump(str_icontains("test string", "string"));
var_dump(str_icontains("test string", "strin"));
var_dump(str_icontains("test string", "t s"));
var_dump(str_icontains("test string", "g"));
var_dump(str_icontains("te".chr(0)."st", chr(0)));
var_dump(str_icontains("tEst", "test"));
var_dump(str_icontains("teSt", "test"));
var_dump(str_icontains("TEST", "test"));
var_dump(str_icontains("test", "TEST"));
var_dump(str_icontains("TESST", "TEST"));
var_dump(str_icontains("", ""));
var_dump(str_icontains("a", ""));
var_dump(str_icontains("", "a"));
var_dump(str_icontains("\\\\a", "\\a"));
?>
--EXPECT--
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(false)
bool(true)
bool(true)
bool(false)
bool(true)