2011-06-30 93 views

回答

17

没有原生的方式,你可以这样做:

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 Top Capital Letter I With Top Above”)。同样,当它移动到小写字母时,我们的大写字母“I”变成“ı”(U + 0131“拉丁小字母无点I”)。

+0

可爱的链接,以测试 – dynamic

+0

我喜欢你的substri_count():) – evilReiko

+1

土耳其测试+1 –

9

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

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

在计数之前做一个strtolower

相关问题