function hook_search_by_page_excerpt_match in Search by Page 8
Find derived forms of keywords for search excerpt (preprocess module hook).
This hook is invoked by \Drupal::service('search_by_page.settings')->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_search_by_page_excerpt_match()
Note: this list is generated by pattern matching, so it may include some functions that are not actually implementations of this hook.
- SearchByPageTesterTest::search_by_page_test_search_by_page_excerpt_match in tests/
src/ Functional/ SearchByPageTesterTest.php - Implements hook_search_by_page_excerpt_match().
1 invocation of hook_search_by_page_excerpt_match()
- SearchByPageService::excerpt in src/
Services/ SearchByPageService.php - Returns a search excerpt, with matched keywords highlighted. The ex-function search_by_page_excerpt($keys, $text) from [D8 ported] .module file.
File
- ./
search_by_page.api.php, line 243 - Search by Page module API.
Code
function hook_search_by_page_excerpt_match($key, $text, $offset, $boundary) {
// Find the root form of the keyword -- in this simple example,
// all but the last 3 characters.
$key = mb_substr($key, 0, -3);
if (mb_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 + mb_strlen($key))) {
$keyfound = mb_substr($text, $pos, $match[0][1] - $pos);
}
return array(
'where' => $pos,
'keyword' => $keyfound,
);
}