evilReiko
evilReiko

Reputation: 20483

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

Just like the question says:

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

Upvotes: 5

Views: 9571

Answers (3)

Karoly Horvath
Karoly Horvath

Reputation: 96266

Do an strtolower before counting.

Upvotes: 2

Gazler
Gazler

Reputation: 84180

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.

Upvotes: 20

Marc B
Marc B

Reputation: 360762

Simple: Coerce both strings to lowercase:

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

Upvotes: 10

Related Questions