Collections functions
General functions
Node functions
Render functions
Theme permission functions
User functions
Resource 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 2399 to 2450

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
    # Optional - depends on $highlightkeywords being set in config.php.
    
global $highlightkeywords$stemming;
    
# Situations where we do not need to do this.
    
if (!isset($highlightkeywords) || ($highlightkeywords==false) || ($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 19th March 2024 13:35 Europe/London time based on the source file dated 15th March 2024 17:00 Europe/London time.