开发者

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

开发者 https://www.devze.com 2023-03-16 19:29 出处:网络
Just like th开发者_如何学Pythone question says: Is there a way to invoke case-insensitive substr_count()?There is not a native way, you can do:

Just like th开发者_如何学Pythone question says:

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


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.


Simple: Coerce both strings to lowercase:

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


Do an strtolower before counting.

0

精彩评论

暂无评论...
验证码 换一张
取 消