In this tutorial Well chat nearly How to highlight the keyword in the string and search result consequences using php, Itll encourage you to focus approaching search keyword input by adherent. With the serve of this rushed php feat you can easily put the accent on matched word as full keyword in search result. You can hence copy and cement taking into consideration achievement out your php file and make it actionable. You only way to appendix text and keyword which you ache to make highlight.

Following is the PHP behave to highlight the keyword in the string.

function highlightKeywords($keyword, $text) {
	if(isset($keyword) && !empty($keyword)) {
		return preg_replace("/\p{L}*?".preg_quote($keyword)."\p{L}*/ui", "<b style='color:blue'>$0</b>", $text);
	} else {
		return false;
$text = "<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s";
echo highlightKeywords('Lorem', $text);

The above do something uses preg_replace to replace matched keyword as soon as some CSS Where \w*? matches any word characters to the lead the keyword (as least as possible) and \w* any word characters after the keyword. So As opinion use preg_quote to control away the keyword.

Leave a Reply

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