Automatically put words for autocompleting

A code example that will automatically put those words for autocompleting that are more often happen in the texts:

<?php
function get_words($texts){
if(!empty($texts)){
$words = array();
$words_count = 0;

foreach($texts as $text){
preg_match_all(“/\w{2,}/i”, $text, $matches, PREG_SET_ORDER);

foreach($matches as $word){
$word_found = false;

if(!empty($words)){
for($i=0; $i<count($words); $i++){
if($words[$i][“word”]==$word[0]){
$words[$i][“count”]++;
$word_found = true;
break;
}
}
}

if(!$word_found){
$words[$words_count][“word”] = $word[0];
$words[$words_count][“count”] = 1;

$words_count++;
}else{
$word_found = false;
}
}
}

return $words;
}else{
return false;
}
}

function sort_by_words_count($word1,$word2){
$word1_count = $word1[“count”];
$word2_count = $word2[“count”];

if($word1_count<$word2_count){
return 1;
}elseif($word1_count==$word2_count){
return 0;
}else{
return -1;
}
}

$text1 = “If you would like to have your running line on your WordPress blog, so this plugin is just for you! It gives you the possibility to show one of the posts from your chosen category by the running line. So download and enjoy!”;

$text2 = “This plugin is used when there are words, words expressions or sentences to be explained in the posts or pages of your wordpress blog. This plugin will help the visitors to read the explanations of the words (words expressions,sentences) you need via tooltips.”;

$text3 = “Any occasion to receive greetings? So, this plugin is just for you! This Widget plugin shows a greeting image which would be uploaded for the occasion and a pop up form for receiving the greetings. It is possible to show the greetings in any post or page.”;

$text4 = “This plugin adds links after the posts for subscribing to the post author’s posts feed via RSS. So if there are several authors in the blog, but a visitor wishes to subscribe just an author posts feed, this plugin gives that possibility to do it.”;

$text5 = “This is a widget plugin which helps to display the visitors who left the most number of comments in the WordPress blog.”;

$texts = array($text1, $text2, $text3, $text4, $text4);

$all_words = get_words($texts);

usort($all_words, ‘sort_by_words_count’);

$autocompleting_words_count = 10; // Autocompleting words count

for($i=0; $i<$autocompleting_words_count; $i++){
$autocompleting_words[$i] = ‘”‘.$all_words[$i][“word”].'”‘;
}

$autocompleting_words = implode(“,”,$autocompleting_words);
?>

<html>
<head>
<link href=”http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css&#8221; rel=”stylesheet” type=”text/css”/>
<script src=”http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js”></script&gt;
<script src=”http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js”></script&gt;

<script>
$(document).ready(function() {
$(“input#autocomplete”).autocomplete({
source: [<?php echo($autocompleting_words); ?>]
});
});
</script>
</head>
<body>

<input id=”autocomplete” />

</body>
</html>

Advertisements
Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: