• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 224
  • Last Modified:

suggestion on how to highlight some word in a block of string

Hello,

I want to implement a function which can make some keywords highlighted. For example:
$kerwordArr=("green", "blue", "yellow");
$stringTest ="the sky is blue, the tree is green, green is the color of life, the soil is yellow"

I want  the words: green, blue, yellow in  the output of the $stringTest can be highlighted in a certain color, i.e. orange

some sample code for implementing the function will be very helpful.

Thanks so much for your suggestion,
lilyyan
0
lilyyan
Asked:
lilyyan
2 Solutions
 
trickyidiotCommented:
<?
for ($i=0;$i<sizeof($kerwordArr);$i++){
  $stringTest = ereg_replace($kerwoodArr,'<span class=\"highlight\">'.$kerwordArr($i).'</span>',$stringTest);
}
?>
0
 
raja_ind82Commented:
<?php

$kerwordArr=array("green", "blue", "yellow");
$stringTest ="the sky is blue, the tree is green, green is the color of life, the soil is yellow";

for ($i=0;$i<sizeof($kerwordArr);$i++){
$findreplace=$kerwordArr[$i];
$replacewith="<b>".$kerwordArr[$i]."</b>";
$stringTest = str_replace($findreplace,$replacewith, $stringTest);
}

echo $stringTest;
 ?>

Regards,
M.Raja
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now