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:

Create a Better WordPress Search Experience Today

Never lose visitors to unhelpful search results again. SearchWP makes creating your own smart WordPress search fast and easy.

Get SearchWP Now
Multiple Search Engines Icon