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()

Parameters

ColumnTypeDefaultDescription
$text
$search
$partial_index false
$field_name ""
$keywords_index 1
$str_highlight_options STR_HIGHLIGHT_SIMPLE

Location

include/search_functions.php lines 2412 to 2462

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 7th September 2024 21:35 Europe/London time based on the source file dated 17th July 2024 15:30 Europe/London time.