ap_highlight_words( string $text, string $words )

Description #

Highlight matching words.

Parameters #

  • $text
    string (Required) String.
  • $words
    string (Required) Words need to highlight.

Changelog #

VersionDescription
2.0Introduced.

Source #

File: includes/functions.php

function ap_highlight_words( $text, $words ) {
	$words = explode( ' ', $words );
	foreach ( $words as $word ) {
		// Quote the text for regex.
		$word = preg_quote( $word, '/' );

		// Highlight the words.
		$text = preg_replace( "/\b($word)\b/i", '<span class="highlight_word">\1</span>', $text );
	}
	return $text;
}

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Add your comment