Find words in the original text that matched via search_simplify().

This is called in search_excerpt() if an exact match is not found in the text, so that we can find the derived form that matches.

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 call to search_simplify_excerpt_match()
search_excerpt in modules/search/search.module
Returns snippets from a piece of text, with certain keywords highlighted. Used for formatting search results.

File

modules/search/search.module, line 1291
Enables site-wide keyword searching.

Code

function search_simplify_excerpt_match($key, $text, $offset, $boundary) {
  $pos = NULL;
  $simplified_key = search_simplify($key);
  $simplified_text = search_simplify($text);

  // Return immediately if simplified key or text are empty.
  if (!$simplified_key || !$simplified_text) {
    return FALSE;
  }

  // Check if we have a match after simplification in the text.
  if (!preg_match('/' . $boundary . $simplified_key . $boundary . '/iu', $simplified_text, $match, PREG_OFFSET_CAPTURE, $offset)) {
    return FALSE;
  }

  // If we get here, we have a match. Now find the exact location of the match
  // and the original text that matched. Start by splitting up the text by all
  // potential starting points of the matching text and iterating through them.
  $split = array_filter(preg_split('/' . $boundary . '/iu', $text, -1, PREG_SPLIT_OFFSET_CAPTURE), '_search_excerpt_match_filter');
  foreach ($split as $value) {

    // Skip starting points before the offset.
    if ($value[1] < $offset) {
      continue;
    }

    // Check a window of 80 characters after the starting point for a match,
    // based on the size of the excerpt window.
    $window = substr($text, $value[1], 80);
    $simplified_window = search_simplify($window);
    if (strpos($simplified_window, $simplified_key) === 0) {

      // We have a match in this window. Store the position of the match.
      $pos = $value[1];

      // Iterate through the text in the window until we find the full original
      // matching text.
      $length = strlen($window);
      for ($i = 1; $i <= $length; $i++) {
        $keyfound = substr($text, $value[1], $i);
        if ($simplified_key == search_simplify($keyfound)) {
          break;
        }
      }
      break;
    }
  }
  return $pos ? array(
    'where' => $pos,
    'keyword' => $keyfound,
  ) : FALSE;
}