我之前写过使用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
我想知道如何将上面的函数转换为返回相同计数的工作 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 中执行此计数函数
感谢您的帮助!!
最佳答案
Counts the number of occurrences of every byte-value (0..255) in string and returns it in various ways.
直接取自 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";
}
?>
您也可以使用它非常轻松地实现您的第二个目的。
关于javascript - PHP计算字符串中字符的唯一性和出现次数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25580549/