Collections functions
General functions
Node functions
Render functions
Theme permission functions
User functions
Resource functions

str_highlight()

Description

Highlight the relevant text in a string

Parameters

ColumnTypeDefaultDescription
$text string Text to search
$needle string Text to highlight
$options int null String highlight options - See include/definitions.php
$highlight string null Optional custom highlight code

Return

string

Location

include/search_functions.php lines 2461 to 2539

Definition

 
function str_highlight($text$needle$options null$highlight null)
    {
    
$text = (string) $text;
    
/*
    this function requires that needle array does not contain any of the following characters: "(" ")"
    */
    
$remove_from_needle = array("("")");
    
$needle str_replace($remove_from_needle""$needle);
    
/*
    Sometimes the text can contain HTML entities and can break the highlighting feature
    Example: searching for "q&a" in a string like "q&a" will highlight the wrong string
    */
    
$htmltext htmlspecialchars_decode($text);
    
// If text contains HTML tags then ignore them
    
if ($htmltext != strip_tags($htmltext))
        {
        
$options $options STR_HIGHLIGHT_STRIPLINKS;
        }

    
# Thanks to Aidan Lister <aidan@php.net>
    # Sourced from http://aidanlister.com/repos/v/function.str_highlight.php on 2007-10-09
    # As of 2020-09-07 code is now at https://github.com/aidanlister/code/blob/master/function.str_highlight.php 
    # The GitHub code repository README states: "The code resides entirely in the public domain."
    # https://github.com/aidanlister/code

    
$text=str_replace("_","♠",$text);// underscores are considered part of words, so temporarily replace them for better \b search.
    
$text=str_replace("#zwspace;","♣",$text);
    
    
// Default highlighting. This used to use '<' and '>' characters as placeholders but now changed as they were being removed by strip_tags
    
if ($highlight === null) {
        
$highlight '\(\1\)';
    }
    
    
// Select pattern to use
    
if ($options STR_HIGHLIGHT_SIMPLE) {
        
$pattern '#(%s)#';
        
$sl_pattern '#(%s)#';
    } else {
        
$pattern '#(?!<.*?)(%s)(?![^<>]*?>)#';
        
$sl_pattern '#<a\s(?:.*?)>(%s)</a>#';
    }
    
    
// Case sensitivity
    
if (!($options STR_HIGHLIGHT_CASESENS)) {
        
$pattern .= 'i';
        
$sl_pattern .= 'i';
    }
    
    
$needle = (array) $needle;

    
usort($needle"sorthighlights");

    foreach (
$needle as $needle_s) {
        if (
strlen($needle_s) > 0) {
            
$needle_s preg_quote($needle_s"#");
        
            
// Escape needle with optional whole word check
            
if ($options STR_HIGHLIGHT_WHOLEWD) {
                
$needle_s '\b' $needle_s '\b';
            }
        
            
// Strip links
            
if ($options STR_HIGHLIGHT_STRIPLINKS) {
                
$sl_regex sprintf($sl_pattern$needle_s);
                
$text preg_replace($sl_regex'\1'$text);
            }
        
            
$regex sprintf($pattern$needle_s);
            
$text preg_replace($regex$highlight$text);
        }
    }
    
$text=str_replace("♠","_",$text);
    
$text=str_replace("♣","#zwspace;",$text);    

    
# Fix - do the final replace at the end - fixes a glitch whereby the highlight HTML itself gets highlighted if it matches search terms, and you get nested HTML.
    
$text=str_replace("\(",'<span class="highlight">',$text);
    
$text=str_replace("\)",'</span>',$text);
    return 
$text;
    }

This article was last updated 19th March 2024 02:35 Europe/London time based on the source file dated 15th March 2024 17:00 Europe/London time.