You are here

function sbp_test_sbp_excerpt_match in Search by Page 7

Same name and namespace in other branches
  1. 6 tests/sbp_test.module \sbp_test_sbp_excerpt_match()

Implements hook_sbp_excerpt_match().

For testing of the search_by_page_excerpt() function.

File

tests/sbp_test.module, line 23
Module file for Search by Page testing.

Code

function sbp_test_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,
  );
}