MyBB Community Forums

Full Version: Keyword Density tool PHP help?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
How to create a textarea where people will add text and then this code will use that text instead of $str

<?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";
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Word density</title>
</head>
<body>
<?php
if(!empty($_POST['str']))
{
	$str = $_POST['str'];
}
else
{
	$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";
}
?>
<form method="post" action="">
<textarea name="str"></textarea>
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>

I haven't tested but I think it should work. Is there a reason you're posting here as this seems more of a web development issue than a MyBB one?
Thank you so much works great! Could you please make it only check keywords with 3 character and more and it would be great if I could add stopwords words I don't want to cont.
I personally have no idea. That sort of question is more suited to a programming question site such as StackOverflow. Why not try asking some experienced web developers there?
Thanks very much though Smile