Collections functions
Encryption functions
General functions
Render functions
Theme permission functions
Video 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

void

Location

include/search_functions.php lines 2173 to 2245

Definition

 
function str_highlight($text$needle$options null$highlight null)
    {
    
/*
    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 25th November 2020 05:35 Europe/London time based on the source file dated 19th November 2020 14:20 Europe/London time.