If you have search something on web and you got a long then how will you highlight your search text here is the answer of this. I have written a PHP program to highlight the multiple keywords in text.
I'm just iterating the whole text and finds the match keyword to be highlighting.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?php function highlightText($text, $keywords) { $color = "yellow"; $background = "red"; foreach($keywords as $keyword) { $highlightWord = "<strong style='background:".$background.";color:".$color."'>" . $keyword . "</strong>"; $text = preg_replace ("/" . trim($keyword) . "/", $highlightWord, $text); } return $text; } $text = "Coding 4 Developers originated for sharing running codes with programmers. The journey was started on 14th January 2016. Coding for developers is not a tutorial website where you can learn step by step. It has running codes, you just can download or copy paste codes for your use."; $keywords = array("Coding 4 Developers","Coding for developers"); echo highlightText($text, $keywords); ?> |