PHP:有没有办法调用不区分大小写的substr_count()?

时间:2011-06-30 21:36:40

标签: php

就像问题所说:

有没有办法调用case- 不敏感 substr_count()?

3 个答案:

答案 0 :(得分:18)

没有本地方式,你可以这样做:

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

你当然可以把它写成一个函数:

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

在使用案例更改来比较字符串时,请注意土耳其测试。

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

从上面开始:

  

正如很多人所讨论的,土耳其语中的“我”与大多数语言的行为不同。根据Unicode标准,当它移动到大写时,我们的小写“i”变为“İ”(U + 0130“Latin Capital Letter I With Dot Above”)。同样,当它移动到小写时,我们的大写“I”变成“ı”(U + 0131“Latin Small Letter Dotless I”)。

答案 1 :(得分:10)

简单:将两个字符串强制为小写:

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

答案 2 :(得分:2)

在计算之前执行strtolower

相关问题