Skip to content

Commit 9778add

Browse files
committed
Create str_icontains PHP function
As a PHP developer of 20 years I'm somewhat accustomed to adding "i" into the function name when I'm after a case-insensitive version. Therefore, I found it a bit odd I couldn't do it with the new "str_contains" function, so have added a case-insensitive version. Tests added too.
1 parent 615b980 commit 9778add

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed

ext/standard/basic_functions.stub.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2439,6 +2439,11 @@ function strrchr(string $haystack, string $needle, bool $before_needle = false):
24392439
*/
24402440
function str_contains(string $haystack, string $needle): bool {}
24412441

2442+
/**
2443+
* @compile-time-eval
2444+
*/
2445+
function str_icontains(string $haystack, string $needle): bool {}
2446+
24422447
/**
24432448
* @compile-time-eval
24442449
* @frameless-function {"arity": 2}

ext/standard/string.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1836,6 +1836,20 @@ ZEND_FRAMELESS_FUNCTION(str_contains, 2)
18361836
Z_FLF_PARAM_FREE_STR(2, needle_tmp);
18371837
}
18381838

1839+
/* {{{ Checks if a string contains another, case insensitive */
1840+
PHP_FUNCTION(str_icontains)
1841+
{
1842+
zend_string *haystack, *needle;
1843+
1844+
ZEND_PARSE_PARAMETERS_START(2, 2)
1845+
Z_PARAM_STR(haystack)
1846+
Z_PARAM_STR(needle)
1847+
ZEND_PARSE_PARAMETERS_END();
1848+
1849+
RETURN_BOOL(php_memnistr(ZSTR_VAL(haystack), ZSTR_VAL(needle), ZSTR_LEN(needle), ZSTR_VAL(haystack) + ZSTR_LEN(haystack)));
1850+
}
1851+
/* }}} */
1852+
18391853
/* {{{ Checks if haystack starts with needle */
18401854
PHP_FUNCTION(str_starts_with)
18411855
{
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
--TEST--
2+
Test str_icontains() function
3+
--FILE--
4+
<?php
5+
var_dump(str_icontains("test string", "test"));
6+
var_dump(str_icontains("test string", "string"));
7+
var_dump(str_icontains("test string", "strin"));
8+
var_dump(str_icontains("test string", "t s"));
9+
var_dump(str_icontains("test string", "g"));
10+
var_dump(str_icontains("te".chr(0)."st", chr(0)));
11+
var_dump(str_icontains("tEst", "test"));
12+
var_dump(str_icontains("teSt", "test"));
13+
var_dump(str_icontains("TEST", "test"));
14+
var_dump(str_icontains("test", "TEST"));
15+
var_dump(str_icontains("TESST", "TEST"));
16+
var_dump(str_icontains("", ""));
17+
var_dump(str_icontains("a", ""));
18+
var_dump(str_icontains("", "a"));
19+
var_dump(str_icontains("\\\\a", "\\a"));
20+
?>
21+
--EXPECT--
22+
bool(true)
23+
bool(true)
24+
bool(true)
25+
bool(true)
26+
bool(true)
27+
bool(true)
28+
bool(true)
29+
bool(true)
30+
bool(true)
31+
bool(true)
32+
bool(false)
33+
bool(true)
34+
bool(true)
35+
bool(false)
36+
bool(true)

0 commit comments

Comments
 (0)