You are here

function keyword_rules_title in SEO Compliance Checker 6.2

Same name and namespace in other branches
  1. 6 keyword_rules/keyword_rules.module \keyword_rules_title()

Checks if keywords are used in the node title. If they are used, the resulting score depends on how early the keywords occur in the title. If a keyword is used as the first word in the title, the score will be 100%.

Parameters

object $form_values:

1 string reference to 'keyword_rules_title'
keyword_rules_register_seo_rules in keyword_rules/keyword_rules.module
Implementation of hook_register_seo_rules().

File

keyword_rules/keyword_rules.module, line 101
Implements some keyword based rules for the SEO Checker.

Code

function keyword_rules_title($form_values, $title_field = 'title') {
  if (!isset($form_values[$title_field])) {
    return FALSE;
  }
  $tags = _keyword_rules_extract_tags($form_values);
  $title = strtolower($form_values[$title_field]);
  $best_score = 1000;

  /* sentinel */
  $best_tag = null;
  foreach ($tags as $tag) {
    if (($score = seo_checker_wordipos($title, $tag)) !== FALSE && $score < $best_score) {
      $best_score = $score;
      $best_tag = $tag;
    }
  }

  /* no tags or tags not in title */
  if (is_null($best_tag)) {
    return 0;
  }

  /* calculate percentage score */
  list($before, $after) = explode($best_tag, $title, 2);
  preg_match_all('/\\W+/', $before, $matches);
  $words_before = count($matches[0]);
  if ($words_before == 0) {
    return 100;
  }
  else {
    preg_match_all('/\\W+/', $after, $matches);

    /* count the keyword itself as one oft he "words_after" */
    $words_after = count($matches[0]) + 1;
    return 100 * $words_after / ($words_before + $words_after);
  }
}