Sort texts by matching the word

An example for sorting texts in array by matching the word in them:

<?php
$word = “plugin”;

function sort_by_matching_word($text1,$text2){
global $word;

$text1_matched_words_count = preg_match(“/\b”.$word.”\b/i”,$text1);
$text2_matched_words_count = preg_match(“/\b”.$word.”\b/i”,$text2);

if($text1_matched_words_count<$text2_matched_words_count){
return 1;
}elseif($text1_matched_words_count==$text2_matched_words_count){
return 0;
}else{
return -1;
}
}

$wordpress_plugin_running_line = “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!”;
$wordpress_plugin_explanatory_dictionary = “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.”;
$wordpress_plugin_greetings = “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.”;

$my_plugins = array($wordpress_plugin_running_line,$wordpress_plugin_explanatory_dictionary,$wordpress_plugin_greetings);

usort($my_plugins, ‘sort_by_matching_word’);

foreach($my_plugins as $my_plugin){
echo(“<p>”.$my_plugin.”</p>”);
}
?>

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 )

w

Connecting to %s

%d bloggers like this: