javascript - PHP计算字符串中字符的唯一性和出现次数

标签 javascript php

我之前写过使用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 中执行此计数函数

感谢您的帮助!!

最佳答案

使用count_chars

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/

相关文章:

javascript - 如果通过特定字符串搜索元素,如何获取 nextsibling 的值?

javascript - 为什么我的不和谐机器人无法正常上线?

javascript - Firefox 扩展 : load script from chrome://url into page

php - 从 jquery ajax 响应中获取额外的输入字符

javascript - 防止用户多次单击 'like' 的最佳方法

php - 有相对复杂的表单和提交流程。需要减肥方面的帮助

java - 从 Hibernate XML 配置文件生成实体类和数据库架构

javascript:在 promise 解决之前不要返回

javascript - instagram 样式探索页面图像列表与 CSS

php - 如何在 Yiibooster 中使用配置 TbEditableColumn?