Подсчитать количество вхождений php

count_chars

Подсчитывает количество вхождений каждого из символов с ASCII-кодами в диапазоне (0..255) в строке string и возвращает эту информацию в различных форматах.

Список параметров

Смотрите возвращаемые значения.

Возвращаемые значения

  • 0 — массив, индексами которого являются ASCII-коды, а значениями — число вхождений соответствующего символа.
  • 1 — то же, что и для 0, но информация о символах с нулевым числом вхождений не включается в массив.
  • 2 — то же, что и для 0, но в массив включается информация только о символах с нулевым числом вхождений.
  • 3 — строка, содержащая все уникальные символы в исследуемой строке.
  • 4 — строка, состоящая из символов, которые не входят в исходную строку.

Список изменений

Версия Описание
8.0.0 До этой версии функция возвращала false в случае возникновения ошибки.

Примеры

Пример #1 Пример использования count_chars()

foreach ( count_chars ( $data , 1 ) as $i => $val ) echo «\»» , chr ( $i ) , «\» встречается в строке $val раз(а).\n» ;
>
?>

Результат выполнения данного примера:

" " встречается в строке 4 раз(а). "." встречается в строке 1 раз(а). "F" встречается в строке 1 раз(а). "T" встречается в строке 2 раз(а). "a" встречается в строке 1 раз(а). "d" встречается в строке 1 раз(а). "e" встречается в строке 1 раз(а). "n" встречается в строке 2 раз(а). "o" встречается в строке 2 раз(а). "s" встречается в строке 1 раз(а). "w" встречается в строке 1 раз(а).

Смотрите также

  • strpos() — Возвращает позицию первого вхождения подстроки
  • substr_count() — Возвращает число вхождений подстроки
Читайте также:  Save as html in word

User Contributed Notes 11 notes

If you have problems using count_chars with a multibyte string, you can change the page encoding. Alternatively, you can also use this mb_count_chars version of the function. Basically it is mode «1» of the original function.

/**
* Counts character occurences in a multibyte string
* @param string $input UTF-8 data
* @return array associative array of characters.
*/
function mb_count_chars ( $input ) $l = mb_strlen ( $input , ‘UTF-8’ );
$unique = array();
for( $i = 0 ; $i < $l ; $i ++) $char = mb_substr ( $input , $i , 1 , 'UTF-8' );
if(! array_key_exists ( $char , $unique ))
$unique [ $char ] = 0 ;
$unique [ $char ]++;
>
return $unique ;
>

$input = «Let’s try some Greek letters: αααααΕεΙιΜμΨψ, Russian: ЙЙЫЫЩН, Czech: ěščřžýáíé» ;
print_r ( mb_count_chars ( $input ) );
//returns: Array ( [L] => 1 [e] => 7 [t] => 4 [‘] => 1 [s] => 5 [ ] => 9 [r] => 3 [y] => 1 [o] => 1 [m] => 1 [G] => 1 [k] => 1 [l] => 1 [:] => 3 [α] => 5 [Ε] => 1 [ε] => 1 [Ι] => 1 [ι] => 1 [Μ] => 1 [μ] => 1 [Ψ] => 1 [ψ] => 1 [,] => 2 [R] => 1 [u] => 1 [i] => 1 [a] => 1 [n] => 1 [Й] => 2 [Ы] => 2 [Щ] => 1 [Н] => 1 [C] => 1 [z] => 1 [c] => 1 [h] => 1 [ě] => 1 [š] => 1 [č] => 1 [ř] => 1 [ž] => 1 [ý] => 1 [á] => 1 [í] => 1 [é] => 1 )
?>

// Require (n) unique characters in a string
// Modification of a function below which ads some flexibility in how many unique characters are required in a given string.

$pass = ‘123456’ ; // true
$pass = ‘111222’ ; // false

function req_unique ( $string , $unique = 3 ) if ( count ( count_chars ( $string , 1 )) < $unique ) echo 'false' ;
>else echo ‘true’ ;
>
>

After much trial and error trying to create a function that finds the number of unique characters in a string I same across count_chars() — my 20+ lines of useless code were wiped for this:

function unichar($string) $two= strtolower(str_replace(‘ ‘, », $string));
$res = count(count_chars($two, 1));
return $res;
>

echo unichar(«bob»); // 2
echo unichar(«Invisibility»); //8
echo unichar(«The quick brown fox slyly jumped over the lazy dog»); //26

I have no idea where this could be used, but it’s quite fun

Checking that two strings are anagram:

function isAnagram ( $string1 , $string2 )
return count_chars ( $string1 , 1 ) === count_chars ( $string2 , 1 );
>

isAnagram ( ‘act’ , ‘cat’ ); // true

count_chars for multibyte supported.

function mb_count_chars ( $string , $mode = 0 )

$result = array_fill ( 0 , 256 , 0 );

for ( $i = 0 , $size = mb_strlen ( $string ); $i < $size ; $i ++) $char = mb_substr ( $string , $i , 1 );
if ( strlen ( $char ) > 1 ) continue;
>

switch ( $mode ) case 1 : // same as 0 but only byte-values with a frequency greater than zero are listed.
foreach ( $result as $key => $value ) if ( $value == 0 ) unset( $result [ $key ]);
>
>
break;
case 2 : // same as 0 but only byte-values with a frequency equal to zero are listed.
foreach ( $result as $key => $value ) if ( $value > 0 ) unset( $result [ $key ]);
>
>
break;
case 3 : // a string containing all unique characters is returned.
$buildString = » ;
foreach ( $result as $key => $value ) if ( $value > 0 ) $buildString .= chr ( $key );
>
>
return $buildString ;
case 4 : // a string containing all not used characters is returned.
$buildString = » ;
foreach ( $result as $key => $value ) if ( $value == 0 ) $buildString .= chr ( $key );
>
>
return $buildString ;
>

// change key names.
foreach ( $result as $key => $value ) $result [ chr ( $key )] = $value ;
unset( $result [ $key ]);
>

This function is great for input validation. I frequently need to check that all characters in a string are 7-bit ASCII (and not null). This is the fastest function I have found yet:

function is7bit ( $string ) // empty strings are 7-bit clean
if (! strlen ( $string )) return true ;
>
// count_chars returns the characters in ascending octet order
$str = count_chars ( $str , 3 );
// Check for null character
if (! ord ( $str [ 0 ])) return false ;
>
// Check for 8-bit character
if ( ord ( $str [ strlen ( $str )- 1 ]) & 128 ) return false ;
>
return true ;
>
?>

Here’s a function to count number of strings in a string. It can be used as a simple utf8-enabled count_chars (but limited to a single mode).

function utf8_count_strings ( $stringChar )
$num = — 1 ;
$lenStringChar = strlen ( $stringChar );

for ( $lastPosition = 0 ;
$lastPosition !== false ;
$lastPosition = strpos ( $textSnippet , $stringChar , $lastPosition + $lenStringChar ))
$num ++;
>

Another approach to counting unicode chars.

function count_chars_unicode ( $str , $x = false ) $tmp = preg_split ( ‘//u’ , $str , — 1 , PREG_SPLIT_NO_EMPTY );
foreach ( $tmp as $c ) $chr [ $c ] = isset( $chr [ $c ]) ? $chr [ $c ] + 1 : 1 ;
>
return is_bool ( $x )
? ( $x ? $chr : count ( $chr ))
: $chr [ $x ];
>

$str = «şeker şeker yâriiiiiiiiiimmmmm» ;
print_r ( count_chars_unicode ( $str , ‘â’ )); // frequency of «â»
print_r ( count_chars_unicode ( $str )); // count of uniq chars
print_r ( count_chars_unicode ( $str , true )); // all chars with own frequency
?>

Outputs;
1
9
Array
(
[ş] => 2
[e] => 4
[k] => 2
[r] => 3
[ ] => 2
[y] => 1
[â] => 1
[i] => 10
[m] => 5
)

Here are some more experiments on this relatively new and extremely handy function.

$string = ‘I have never seen ANYTHING like that before! My number is «4670-9394».’ ;

foreach( count_chars ( $string , 1 ) as $chr => $hit )
echo ‘The character ‘ . chr ( 34 ). chr ( $chr ). chr ( 34 ). ‘ has appeared in this string ‘ . $hit . ‘ times.’ ;

#The result looks like
#The character » » has appeared in this string 11 times.

echo count_chars ( $string , 3 );
#The output is ‘!»-.034679AGHIMNTYabefhiklmnorstuvy’

echo strlen ( $string ). ‘ is not the same as ‘ . strlen ( count_chars ( $string , 3 ));

#This shows that ’70 is not the same as 36′
?>

As we can see above:

1)If you cares only about what is in the string, use count_chars($string, 1) and it will return an (associative?) array of what shows up only.

2) Either I misunderstood what the manul actually said, or it does not work the way it described: count_chars($strting, 3) actually returned a string of what characters are in the string, not a string of their byte-values (which is great because a string of numbers would be much harder to handle);

3)This is a short version of password checking: get the original string’s length, then compare with the length of the string returned by count_chars($string,3).

$length_of_string = strlen ( $string );
$num_of_chars = strlen ( count_chars ( $string , 3 ));

$diff = ( $length_of_string — $num_of_chars );

if ( $diff )
echo ‘At least one character has been used more than once.’ ;
else
echo ‘All character have been used only once.’ ;
?>

Note that since $num_of_chars gives no information about the actual number of occurance, we cannot go any further by the same rationale and say when $diff =2 then 2 characters showed up twice; it might be 1 character showd up 3 times, we have no way to tell (a good tolerance level setter, though). You have to get the array and check the values if you want to have more control.

4) Final trick: now we have a primitive way to count the number of words in a string! (or do we have a fuction for that already?)

Источник

substr_count

substr_count() возвращает число вхождений подстроки needle в строку haystack . Заметьте, что параметр needle чувствителен к регистру.

Замечание:

Эта функция не подсчитывает перекрывающиеся подстроки. Смотрите пример ниже!

Список параметров

Строка, в которой ведется поиск

Максимальная длина строки в которой будет производится поиск подстроки после указанного смещения. Если сумма смещения и максимальной длины будет больше длины haystack , то будет выведено предупреждение.

Возвращаемые значения

Список изменений

Версия Описание
5.1.0 Добавлены параметры offset и length

Примеры

Пример #1 Пример использования substr_count()

$text = ‘This is a test’ ;
echo strlen ( $text ); // 14

echo substr_count ( $text , ‘is’ ); // 2

// строка уменьшается до ‘s is a test’, поэтому вывод будет 1
echo substr_count ( $text , ‘is’ , 3 );

// текст уменьшается до ‘s i’, поэтому вывод будет 0
echo substr_count ( $text , ‘is’ , 3 , 3 );

// генерирует предупреждение, так как 5+10 > 14
echo substr_count ( $text , ‘is’ , 5 , 10 );

// выводит только 1, т.к. перекрывающиеся подстроки не учитываются
$text2 = ‘gcdgcdgcd’ ;
echo substr_count ( $text2 , ‘gcdgcd’ );
?>

Смотрите также

  • count_chars() — Возвращает информацию о символах, входящих в строку
  • strpos() — Возвращает позицию первого вхождения подстроки
  • substr() — Возвращает подстроку
  • strstr() — Находит первое вхождение подстроки

Источник

mb_substr_count

Подсчитывает, сколько раз подстрока needle встречается в строке haystack .

Список параметров

Строка ( string ) для проверки

Строка ( string ) для поиска

Параметр encoding представляет собой символьную кодировку. Если он опущен или равен null , вместо него будет использовано значение внутренней кодировки.

Возвращаемые значения

Количество вхождений подстроки needle в строку haystack .

Список изменений

Примеры

Пример #1 Пример использования mb_substr_count()

Смотрите также

  • mb_strpos() — Поиск позиции первого вхождения одной строки в другую
  • mb_substr() — Возвращает часть строки
  • substr_count() — Возвращает число вхождений подстроки

User Contributed Notes

  • Функции для работы с многобайтовыми строками
    • mb_​check_​encoding
    • mb_​chr
    • mb_​convert_​case
    • mb_​convert_​encoding
    • mb_​convert_​kana
    • mb_​convert_​variables
    • mb_​decode_​mimeheader
    • mb_​decode_​numericentity
    • mb_​detect_​encoding
    • mb_​detect_​order
    • mb_​encode_​mimeheader
    • mb_​encode_​numericentity
    • mb_​encoding_​aliases
    • mb_​ereg_​match
    • mb_​ereg_​replace_​callback
    • mb_​ereg_​replace
    • mb_​ereg_​search_​getpos
    • mb_​ereg_​search_​getregs
    • mb_​ereg_​search_​init
    • mb_​ereg_​search_​pos
    • mb_​ereg_​search_​regs
    • mb_​ereg_​search_​setpos
    • mb_​ereg_​search
    • mb_​ereg
    • mb_​eregi_​replace
    • mb_​eregi
    • mb_​get_​info
    • mb_​http_​input
    • mb_​http_​output
    • mb_​internal_​encoding
    • mb_​language
    • mb_​list_​encodings
    • mb_​ord
    • mb_​output_​handler
    • mb_​parse_​str
    • mb_​preferred_​mime_​name
    • mb_​regex_​encoding
    • mb_​regex_​set_​options
    • mb_​scrub
    • mb_​send_​mail
    • mb_​split
    • mb_​str_​split
    • mb_​strcut
    • mb_​strimwidth
    • mb_​stripos
    • mb_​stristr
    • mb_​strlen
    • mb_​strpos
    • mb_​strrchr
    • mb_​strrichr
    • mb_​strripos
    • mb_​strrpos
    • mb_​strstr
    • mb_​strtolower
    • mb_​strtoupper
    • mb_​strwidth
    • mb_​substitute_​character
    • mb_​substr_​count
    • mb_​substr

    Источник

Оцените статью