人人人妻人人人妻人人人,99精品国产综合久久久久五月天 ,欧美白人最猛性XXXXX,日韩AV无码免费播放

News新聞

業(yè)界新聞動態(tài)、技術(shù)前沿
Who are we?

您的位置:首頁      樂道系統(tǒng)FAQ      PHP編程計算文件或數(shù)組中單詞出現(xiàn)頻率的方法

PHP編程計算文件或數(shù)組中單詞出現(xiàn)頻率的方法

標(biāo)簽: 發(fā)布日期:2017-05-22 00:00:00 262

本文實例講述了PHP編程計算文件或數(shù)組中單詞出現(xiàn)頻率的方法。分享給大家供大家參考,具體如下:

如果是小文件,可以一次性讀入到數(shù)組中,使用方便的數(shù)組計數(shù)函數(shù)進(jìn)行詞頻統(tǒng)計(假設(shè)文件中內(nèi)容都是空格隔開的單詞):

<?php
$str = file_get_contents("/path/to/file.txt"); //get string from file
preg_match_all("/\b(\w+[-]\w+)|(\w+)\b/",$str,$r); //place words into array $r - this includes hyphenated words
$words = array_count_values(array_map("strtolower",$r[0])); //create new array - with case-insensitive count
arsort($words); //order from high to low
print_r($words)