SearchWP Documentation

View the installation guide, browse the Knowledge Base, find out about SearchWP’s many hooks

\SearchWP\Highlighter

Table of Contents

\SearchWP\Highlighter can highlight strings within strings.

Basic Usage

All usage of \SearchWP\Highlighter requires two parameters, a $haystack and a $needle. A $needle within a $haystack will be wrapped in a <mark class="searchwp-highlight"/> element.

<?php
// @link https://searchwp.com/documentation/classes/searchwp-highlighter/
$highlighter = new \SearchWP\Highlighter();
$haystack = 'The best coffee you can find!';
$needle = 'coffee';
// Output:
// The best <mark class="searchwp-highlight">coffee</mark> you can find!
echo $highlighter->apply( $haystack, $needle );

Methods

apply( string $haystack, string $needle)
Highlight the $needle in the $haystack

Hooks

There are a number of hooks available to further modify the behavior of \SearchWP\Highlighter:

A Better WordPress Search Experience

Get the best WordPress search plugin. Trusted by 50,000+ websites.

Get SearchWP Now
Multiple Search Engines Icon