What Is the Most Efficient Way to Count All the Occurrences of a Specific Character in a PHP String

Counting the number of specific characters inside string

A possible solution using regular expression:

function get_num_chars($char) {
$string = '120201M, 121212M-1, 21121212M, 232323M-2, 32323K, 323232K-1';
return preg_match_all('/'.$char.'(?=,|$)/', $string, $m);
}

echo get_num_chars('M'); // 2
echo get_num_chars('M-1'); // 1
echo get_num_chars('M-2'); // 1
echo get_num_chars('K'); // 1
echo get_num_chars('K-1'); // 1

PHP count of occurrences of characters of a string within another string

You can calculate the length of the original string and the length of the string without these characters. The differences between them is the number of matches.

Basically,

$needle = 'AGUXYZ';
$haystack = 'Agriculture ID XYZ-A';

Here is the part that does the work. In one line.

$count = strlen($haystack) - strlen(str_replace(str_split($needle), '', $haystack));

Explanation: The first part is self-explanatory. The second part is the length of the string without the characters in the $needle string. This is done by replacing each occurrences of any characters inside the $needle with a blank string.

To do this, we split $needle into an array, once character for each item, using str_split. Then pass it to str_replace. It replaces each occurence of any items in the $search array with a blank string.

Echo it out,

echo "Count = $count\n";

you get:

Count = 5

Find most common character in PHP String

You can use this function,

function getHighest($str){
$str = str_replace(' ', '', $str);//Trims all the spaces in the string
$arr = str_split(count_chars($str.trim($str), 3));
$hStr = "";
$occ = 0;

foreach ($arr as $value) {
$oc = substr_count ($str, $value);
if($occ < $oc){
$hStr = $value;
$occ = $oc;
}
}

return $hStr;
}

How to count only dash sign (-) in string using php?

The most simple is use the function substr_count()

$str = 'some-class-of-css';
echo substr_count($str, '-'); //3

How to get the count of string 2 occurrence in string 1 without php built-in functions

$strone = 'arun sukumar';
$strtwo = 'a';

echo parsestr($strone, $strtwo);

function parsestr($strone, $strtwo)
{
$len = 0;
while ($strtwo{$len} != '') {
$len++;
}

$nr = 0;

while ($strone{$nr} != '')
{
if($strone{$nr} != ' ')
{
$data[$nr] = $strone{$nr};
}
$nr++;
}

$newdata = $data;

if($len > 1)
{
$newdata = array();
$j = 0;
foreach($data as $val)
{
$str .= $val;
if($j == ($len -1))
{
$newdata[] = $str;
$str = '';
$j = 0;
}
else
$j++;
}
}
$i = 0;

foreach($newdata as $val)
{
if($val == $strtwo)
{
$i++;
}
}
return $i;
}

Count characters and get percentage of occurrence in Php or SQL

First split all letters in an array. Count the array. Loop through the array and count how often we see them. Then loop another time to print out the result:

<?php
$text = 'This is a test';
$text = str_replace(' ', '', $text );
$arrLetters = str_split($text);
$countLetters = count($arrLetters);

$letters = [];

foreach($arrLetters as $letter){
if(isset($letters[$letter])){
$letters[$letter] += 1;
} else {
$letters[$letter] = 1;
}
}

foreach($letters as $letter => $total){
echo $letter.":".$total.":".round(($total/$countLetters*100),2)."%<br />";
}

Result:

T:1:9.09%
h:1:9.09%
i:2:18.18%
s:3:27.27%
a:1:9.09%
t:2:18.18%
e:1:9.09%

Count the number of occurrences of a character in a string in Javascript

I have updated this answer. I like the idea of using a match better, but it is slower:

console.log(("str1,str2,str3,str4".match(/,/g) || []).length); //logs 3

console.log(("str1,str2,str3,str4".match(new RegExp("str", "g")) || []).length); //logs 4


Related Topics



Leave a reply



Submit