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){
  • Share/Bookmark
1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5.00 out of 5)
Loading ... Loading ...

Leave a Reply