什么是关键字密度以及如何在PHP中创建脚本? [英] What is Keyword Density and how to create a script in PHP?
问题描述
我正在一个项目中,我必须根据该页面的URL找出该页面的关键字密度.我在Google上搜索了很多,但没有找到帮助和脚本,我找到了付费工具 http://www.selfseo.com/store/_catalog/php_scripts/_keyword_density_checker_php_script
I am working on a project where I have to find out the keyword density of thepage on the basis of URL of that page. I googled a lot but no help and scripts were found, I found a paid tool http://www.selfseo.com/store/_catalog/php_scripts/_keyword_density_checker_php_script
但是我实际上并不知道页面的关键字密度"实际上是什么意思?并且请告诉我如何创建一个PHP脚本来获取网页的关键字密度.
But I am not aware actually what "keyword Density of a page" actually means? and also please tell me how can we create a PHP script which will fetch the keyword density of a web page.
谢谢
推荐答案
关键字密度"是指单词出现的频率,以占单词总数的百分比表示.以下PHP代码将输出字符串$str
中每个单词的密度.它说明了关键字密度并不是一个复杂的计算,可以在几行PHP中完成:
"Keyword density" is simply the frequency that the word occurs given as a percentage of the total number of words. The following PHP code will output the density of each word in a string, $str
. It demonstrates that keyword density is not a complex calculation, it can be done in a few lines of PHP:
<?php
$str = "I am working on a project where I have to find out the keyword density of the page on the basis of URL of that page. But I am not aware actually what \"keyword Density of a page\" actually means? and also please tell me how can we create a PHP script which will fetch the keyword density of a web page.";
// str_word_count($str,1) - returns an array containing all the words found inside the string
$words = str_word_count(strtolower($str),1);
$numWords = count($words);
// array_count_values() returns an array using the values of the input array as keys and their frequency in input as values.
$word_count = (array_count_values($words));
arsort($word_count);
foreach ($word_count as $key=>$val) {
echo "$key = $val. Density: ".number_format(($val/$numWords)*100)."%<br/>\n";
}
?>
示例输出:
of = 5. Density: 8%
a = 4. Density: 7%
density = 3. Density: 5%
page = 3. Density: 5%
...
要获取网页的内容,可以使用 file_get_contents (或 cURL ).例如,以下PHP代码列出了此网页上密度高于1%的所有关键字:
To fetch the content of a webpage you can use file_get_contents (or cURL). As an example, the following PHP code lists all keywords above 1% density on this webpage:
<?php
$str = strip_tags(file_get_contents("http://stackoverflow.com/questions/819166"));
$words = str_word_count(strtolower($str),1);
$word_count = array_count_values($words);
foreach ($word_count as $key=>$val) {
$density = ($val/count($words))*100;
if ($density > 1)
echo "$key - COUNT: $val, DENSITY: ".number_format($density,2)."%<br/>\n";
}
?>
我希望这会有所帮助.
这篇关于什么是关键字密度以及如何在PHP中创建脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!