Coding standards
Security in ResourceSpace
Developer reference
Database
Action functions
Admin functions
Ajax functions
Annotation functions
API functions
Collections functions
Comment functions
Config functions
CSV export functions
Dash functions
Debug functions
Encryption functions
Facial recognition functions
File functions
General functions
Language functions
Log functions
Login functions
Message functions
Migration functions
Node functions
PDF functions
Plugin functions
Render functions
Reporting functions
Request functions
Research functions
Slideshow functions
Theme permission functions
User functions
Video functions
Database functions
Metadata functions
Resource functions
Search functions
Map functions
Job functions
Tab functions
Test functions

highlightkeywords()

Description

Highlights keywords in the given text based on the search query.

This function scans the provided text for keywords specified in the search string
and wraps them in HTML markup to highlight them.

Parameters

ColumnTypeDefaultDescription
$text string The text in which to highlight keywords.
$search string The search query containing keywords to highlight.
$partial_index bool false Indicates whether partial indexing is used (default: false).
$field_name string "" Optional. The name of the field being searched.
$keywords_index int 1 Indicates the indexing status of the field (default: 1).
$str_highlight_options int STR_HIGHLIGHT_SIMPLE Options for highlighting (default: STR_HIGHLIGHT_SIMPLE).

Return

string The text with highlighted keywords.

Location

include/search_functions.php lines 2538 to 2588

Definition

 
function highlightkeywords($text,$search,$partial_index=false,$field_name="",$keywords_index=1$str_highlight_options STR_HIGHLIGHT_SIMPLE)
    {
    global 
$noadd;
    
# do not highlight if the field is not indexed, so it is clearer where results came from.
    
if ($keywords_index!=1)
        {
        return 
$text;
        }

    
# Highlight searched keywords in $text
    
global $stemming;
    
# Situations where we do not need to do this.
    
if ($search=="" || $text=="") {return $text;}

        
# Generate the cache of search keywords (no longer global so it can test against particular fields.
        # a search is a small array so I don't think there is much to lose by processing it.
        
$hlkeycache=array();
        
$s=split_keywords($search);
        for (
$n=0;$n<count($s);$n++)
            {
            if (
strpos($s[$n],":")!==false)
                {
                
$c=explode(":",$s[$n]);
                
// Only add field specific keywords
                
if($field_name!="" && $c[0]==$field_name)
                    {
                    
$hlkeycache[]=$c[1];
                    }
                }
            else
                {
                
// Add general keywords
                
$keyword=$s[$n];
                if (
in_array($keyword$noadd)) # skip common words that are excluded from indexing
                    
{
                    continue;
                    }
                if (
$stemming && function_exists("GetStem")) // Stemming enabled. Highlight any words matching the stem.
                    
{
                    
$keyword=GetStem($keyword);
                    }
                if (
strpos($keyword,"*")!==false)
                    {
                    
$keyword=str_replace("*","",$keyword);
                    }
                
$hlkeycache[]=$keyword;
                }
            }
    
# Parse and replace.
    
return str_highlight($text$hlkeycache$str_highlight_options);
    }

This article was last updated 17th November 2024 15:35 Europe/London time based on the source file dated 8th November 2024 11:45 Europe/London time.