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

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 2599 to 2691

Definition

 
function str_highlight($text$needle$options null$highlight null)
    {
    
$text = (string) $text;

    
# If search text contains HTML entities, convert the search phrase to allow matching of entities e.g. &
    
if (htmlspecialchars_decode($text) != $text)
        {
        if (
is_array($needle))
            {
            
$needle array_map('escape'$needle);
            }
        else
            {
            
$needle escape($needle);
            }
        }

    
/*
    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 4th December 2024 08:35 Europe/London time based on the source file dated 27th November 2024 09:40 Europe/London time.