Sort texts by matching the word

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

$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);

return 1;
return 0;
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){

Leave a comment

Leave a Reply

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

You are commenting using your 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: