You are here

function hook_sbp_excerpt_match in Search by Page 6

Same name and namespace in other branches
  1. 7 search_by_page.api.php \hook_sbp_excerpt_match()

Find derived forms of keywords for search excerpt (preprocess module hook).

This hook is invoked by search_by_page_excerpt() to allow stemming and other search preprocessing modules to find derived forms of keywords to highlight, when creating a "snippet" for search results display, if the given keyword is not found directly in the text.

Parameters

$key: The keyword to find.

$text: The text to search for the keyword.

$offset: Offset position in $text to start searching at.

$boundary: Text to include in a regular expression that will match a word boundary.

Return value

FALSE if no match is found. If a match is found, return an associative array with element 'where' giving the position of the match, and element 'keyword' giving the actual word found in the text at that position.

1 function implements hook_sbp_excerpt_match()

Note: this list is generated by pattern matching, so it may include some functions that are not actually implementations of this hook.

sbp_test_sbp_excerpt_match in tests/sbp_test.module
Implementation of hook_sbp_excerpt_match().
1 invocation of hook_sbp_excerpt_match()
search_by_page_excerpt in ./search_by_page.module
Returns a search excerpt, with matched keywords highlighted.

File

./search_by_page.api.php, line 240
Search by Page module API.

Code

function hook_sbp_excerpt_match($key, $text, $offset, $boundary) {

  // Find the root form of the keyword -- in this simple example,
  // all but the last 3 characters.
  $key = drupal_substr($key, 0, -3);
  if (drupal_strlen($key) < 3) {
    return FALSE;
  }

  // Look for this modified key at the start of a word.
  $match = array();
  if (!preg_match('/' . $boundary . $key . '/iu', $text, $match, PREG_OFFSET_CAPTURE, $offset)) {

    // didn't match our modified key.
    return FALSE;
  }

  // If we get here, we have a match. Find the end of the word we
  // actually matched, so it can be highlighted.
  $pos = $match[0][1];
  if (preg_match('/' . $boundary . '/iu', $text, $match, PREG_OFFSET_CAPTURE, $pos + drupal_strlen($key))) {
    $keyfound = drupal_substr($text, $pos, $match[0][1] - $pos);
  }
  return array(
    'where' => $pos,
    'keyword' => $keyfound,
  );
}