0

How to change normal sentence WordPress title to "tag like" list with excluded "stop words".

Example:

"This is one test post title about something."

To:

"one, test, post, title, something"

This is my function which will exclude blacklist words (stop words), and words with less than 3 characters (usually stop words too).

<?php    
// Post title as keywords, excluded stop words
function title_as_keywords( $word )
{
$blacklist = array('this', 'about', 'that', 'them'); // Excluded words ...
    if ( !in_array( $word, $blacklist ) && strlen( $word ) > 3 ) { //Exclude words with less than 4 characters
          $word_with_comma = '' .$word. ', '; // Separate words with comma
          return $word_with_comma;
      }
    else { return ''; } // Return nothing for black list words or words with less than 4 characters

}
?>

<?php
$title = get_the_title( $post->post_parent ); // Get title
$parts = explode( ' ', $title );
echo title_as_keywords( $parts[0] );
echo title_as_keywords( $parts[1] );
echo title_as_keywords( $parts[2] );
echo title_as_keywords( $parts[3] );
//...
?>

Problems: This is working fine but it needs fine tuning. This will give this result:

"test, post, title, something, "

Problem 1:

I would like to display last word without ", " at the end.

Problem 2:

It will exclude word "one" (because it is 3 characters long), I can set it to include words with 3 characters, but than I will have to create long blacklist to include 3 characters stop words (the, who, she, are, his, her...).

3
  • 1
    You could loop through your $parts with a foreach and then trim the last character of your string that will be a ",". You could also create a second array of whitelisted <= 3 characters words.
    – Sladix
    Jan 15 '16 at 9:11
  • Yes, that was my idea, to create whitelisted words, for example (red, man, boy...). Also I tried to trim ", " in last $part, but I probably made some mistake or something was wrong in my logic. Could you please update my function with added white list and with last word without ", " so I can see that? Thank you @Antoine Guillien Jan 15 '16 at 10:02
  • Voted to migrate to Stack Overflow as this is a generic php issue Jan 15 '16 at 10:50
1

Here is your updated function solving your 2 problems :

// Post title as keywords, excluded stop words
function title_as_keywords( $word )
{
    $blacklist = array('this', 'about', 'that', 'them'); // Excluded words ...
    $whitelist = array('one', 'man', 'boy'/*etc..*/); // Whitelisted words ...
    if ( (!in_array( $word, $blacklist ) && strlen( $word ) > 3 ) || in_array($word, $whitelist)) { //Exclude words with less than 4 characters
          $word_with_comma = '' .$word. ', '; // Separate words with comma
          return $word_with_comma;
      }
    else { return ''; } // Return nothing for black list words or words with less than 4 characters

}
$title = get_the_title( $post->post_parent ); // Get title
$parts = explode( ' ', $title );
$str = '';
foreach ($parts as $word) {
    $str.= title_as_keywords($word);
}
$str = substr($str, 0,-2);
echo $str;

Hope it'll help :)

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.