Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: is there a way to invoke case-insensitive substr_count()?

Tags:

php

Just like the question says:

Is there a way to invoke case-insensitive substr_count()?

like image 243
evilReiko Avatar asked Jun 30 '11 21:06

evilReiko


People also ask

Is Substr_count case-sensitive?

?> The substr_count() function counts the number of times a substring occurs in a string. Note: The substring is case-sensitive.

How do you make a string case-insensitive in PHP?

The strcasecmp() function is a built-in function in PHP and is used to compare two given strings. It is case-insensitive. This function is similar to strncasecmp(), the only difference is that the strncasecmp() provides the provision to specify the number of characters to be used from each string for the comparison.

Is Str_contains case-sensitive?

str_contains() function is case-sensitive. There is no case-insensitive variant of this function.

Are PHP commands case-sensitive?

In PHP, class names as well as function/method names are case-insensitive, but it is considered good practice to them functions as they appear in their declaration.


1 Answers

There is not a native way, you can do:

substr_count(strtoupper($haystack), strtoupper($needle));

You can of course write this as a function:

function substri_count($haystack, $needle)
{
    return substr_count(strtoupper($haystack), strtoupper($needle));
}

Be aware of the Turkey test when using case changes to compare strings.

http://www.moserware.com/2008/02/does-your-code-pass-turkey-test.html

From the above:

As discussed by lots and lots of people, the "I" in Turkish behaves differently than in most languages. Per the Unicode standard, our lowercase "i" becomes "İ" (U+0130 "Latin Capital Letter I With Dot Above") when it moves to uppercase. Similarly, our uppercase "I" becomes "ı" (U+0131 "Latin Small Letter Dotless I") when it moves to lowercase.

like image 177
Gazler Avatar answered Nov 15 '22 03:11

Gazler