bhagwant Singh
bhagwant Singh

Reputation: 3

How to change color of text if it is in array php

I am using this code to change the color of search keyword. But due to searching multiple words from query I create $keyword as array. So now how can I use this?

<?php echo preg_replace("/\p{L}*?".preg_quote($keyword)."\p{L}*/ui", "<span class='changecoolor'>$0</span>", $row['ti_name']); ?>

Upvotes: 0

Views: 268

Answers (1)

Barmar
Barmar

Reputation: 780889

To match multiple words in a regexp, separate them with | and put them into a group.

$keyword_regexp = '(?:' . implode('|', array_map('preg_quote', $keyword)) . ')';
echo preg_replace("/\p{L}*?$keyword_regexp\p{L}*/ui", "<span class='changecoolor'>$0</span>", $row['ti_name']);

Upvotes: 1

Related Questions