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

Posted on 2007-08-03
Last Modified: 2013-11-05

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,
Question by:lilyyan
    LVL 6

    Accepted Solution

    for ($i=0;$i<sizeof($kerwordArr);$i++){
      $stringTest = ereg_replace($kerwoodArr,'<span class=\"highlight\">'.$kerwordArr($i).'</span>',$stringTest);
    LVL 14

    Assisted Solution


    $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++){
    $stringTest = str_replace($findreplace,$replacewith, $stringTest);

    echo $stringTest;


    Featured Post

    Courses: Start Training Online With Pros, Today

    Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

    Join & Write a Comment

    Both Easy and Powerful How easy is PHP? (  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
    This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
    The viewer will learn how to dynamically set the form action using jQuery.
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now