Answer the question
In order to leave comments, you need to log in
Answer the question
In order to leave comments, you need to log in
To put it simply, you can do something like this.
$array = array_filter(explode(' ', $string));
$count_array = array_count_values($array);
var_dump($count_array);
Why make it so difficult? There are also standard functions for most of the trivial tasks.
str_word_count — Returns information about the words in a string
$str=file_get_contents('./words.txt');
$aw=preg_split('/[\W]+/', $str);
$wc=array_count_values($aw);
file_put_contents('./words_out.txt', var_export(array($aw, $wc), true));
php. Как получить массив слов строки?
PHP*
Здравствуйте. Не подскажете как из строки получить массив слов и количество вхождений?
php
array (
0 =>
array (
0 => 'php',
1 => 'Как',
2 => 'получить',
3 => 'массив',
4 => 'слов',
5 => 'строки',
6 => 'PHP',
7 => 'Здравствуйте',
8 => 'Не',
9 => 'подскажете',
10 => 'как',
11 => 'из',
12 => 'строки',
13 => 'получить',
14 => 'массив',
15 => 'слов',
16 => 'и',
17 => 'количество',
18 => 'вхождений',
19 => 'php',
),
1 =>
array (
'php' => 2,
'Как' => 1,
'получить' => 2,
'массив' => 2,
'слов' => 2,
'строки' => 2,
'PHP' => 1,
'Здравствуйте' => 1,
'Не' => 1,
'подскажете' => 1,
'как' => 1,
'из' => 1,
'и' => 1,
'количество' => 1,
'вхождений' => 1,
),
)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question