2014-08-30 17 views
0

我以前寫的使用javascript腓計算字符串獨特次數焦炭

這是我的功能

var check = {}, string = inputString, count = 0; 
var occurrence; 
$.each(string.split(''), function(){ 
    if(!check[this]){ 
     count++; 
     check[this]=true; 
}) 

來計算客戶端功能上面我用它來計算出一個串號發生

例如

number 1234 will return 4, as there are number 1,2,3 and 4 
number 1233 will return 3, as the unique number are 1,2 and 3 
number 1212 will return 2, as the unique number are 1 and 2 

我不知道我怎麼轉換我的F上面的聯合到一個工作的PHP函數返回相同的計數。

接下來的事情就是我寫的另一項檢查是getMostOccurrence

的目的是爲了例如

number 1212, will return 2, as 1 appear 2 times, and 2 appear 2 times 

然而,如果數

1112, the result will be 3, as 1 appear 3 times 

我該怎麼做在PHP中,這計數功能

感謝您的幫助!

回答

2

使用count_chars

計數字符串中的每個字節的值(0..255)的出現次數,並返回它以不同的方式。從PHP.net

<?php 
$data = "Two Ts and one F."; 

foreach (count_chars($data, 1) as $i => $val) { 
    echo "There were $val instance(s) of \"" , chr($i) , "\" in the string.\n"; 
} 
?> 

直取

例子你可以用它來實現你的第二個目的,很容易爲好。

+0

我用count char,他們給我這個字符串中有1個「A」的實例。字符串中有1個「a」實例。字符串中有2個「r」的實例。字符串中有1個「y」實例。對於1234 – 2014-08-30 08:57:27

+0

可能你沒有正確使用它。看到這裏https://eval.in/185748 – 2014-08-30 14:00:56