Home>Article>Backend Development> Three ways to get the number of occurrences of characters in a string in php
This article mainly introduces the method for PHP to obtain the number of occurrences of each character in a string. It analyzes three common techniques for PHP to obtain the number of occurrences of characters in a string. It has certain reference value. Friends who need it can refer to it.
The example in this article describes how PHP obtains the number of occurrences of each character in a string.
The specific implementation method is as follows:
"; print_r($arr); //方法二 $arr = str_split($str); $con = array(); foreach ($arr as $v){ if(!@$con[$v]){ $con[$v]=1; }else{ $con[$v]++; } } arsort($con); print_r($con); //方法三 $arr = str_split($str); $unique = array_unique($arr); //移除数组中重复的值,并返回结果数组(键名不变); print_r($unique); foreach($unique as $v){ $arr2[$v] = substr_count($str,$v); //substr_count():计算某字符在字符串中出现的次数 } arsort($arr2); print_r($arr2); ?>
Summary: The above is the entire content of this article, I hope it will be helpful to everyone's learning.
Related recommendations:
PHP implements the function of detecting GSM characters in mobile phone text messages
The function and usage of strpos in php
php method to remove empty characters in a string
The above is the detailed content of Three ways to get the number of occurrences of characters in a string in php. For more information, please follow other related articles on the PHP Chinese website!