利用PHP 的 str_word_count 和 array_walk 制作页面的密度查询
<?php
function str_exists($item,$key,$keyword){
global $i;
if(preg_match("/$keyword/i",$item)){
$i++;
}
}
function clean_html($str){
return preg_replace("/<\/[^>]+>|<[^\s]+\s|\'|\"|<[a-z]+>/i",'',$str);
}
function count_density($item,$key,$sum){
if($item>0)
echo " $item times of $key, with the frequency of".number_format(($item/$sum)*100,2)."
";
}
$site=$_POST['site'];
if(strpos($site,'http://')===false){
$site="http://".$site;
}
$keyword=trim($_POST['keyword']);
$str=file_get_contents($site);
$str1=clean_html($str);
$a1=str_word_count($str1,1,$keyword);//获得所有的英语单词
arsort($a1);
$i=0;
array_walk($a1,'str_exists',$keyword);
$coo=count($a1);
echo "$i times of $keyword,with the frequency of :".number_format(($i/$coo)*100,2).'%';
?>