Jump to content

[SOLVED] How do I replace more than one word at a time in a string?


HowdeeDoodee

Recommended Posts

I want to replace/highlight more than one word in a string. How do I do it? The following code works great but it only highlights/replaces one word in the string.

 

Thank you in advance for any replies.

 

<?

function highlight_search_criteria($search_results, $search_criteria, $bgcolor='Yellow') 
{ 
//echo $search_criteria;
    if (empty($search_criteria)) { 
       return $search_results; 
    } else { 
       $start_tag = "<span style='background-color: $bgcolor'>";
       $end_tag = '</span>';
       $highlighted_results = $start_tag . $search_criteria . $end_tag;
      return eregi_replace($search_criteria, $highlighted_results, $search_results);
   } 
} 

$term;
$term[0] = 'John Doe';
$term[1] = 'Jane Smith';
$term[2] = 'Bill Bobbins';

foreach( $term as $value)
{

$bgcolor='Yellow';
$start_tag = "<span style='background-color: $bgcolor'>";
$end_tag = '</span>';

$db_result_text = "The users with the most points in this game were John Doe, Jane Smith, and Bill Bobbins."; 

$result_text = highlight_search_criteria($db_result_text, $value); 
}

//result_text is the sentence or string with a highlighted word
echo " result_text =     ", $result_text;

?>

Link to comment
Share on other sites

Rather than do a foreach loop outside of the function do it inside the function instead. Just pass your search terms as an array to your highlight_search_criteria function. SO your new code:

<?php

function highlight_search_criteria($search_results, $search_criteria, $bgcolor='Yellow')
{
$start_tag = "<span style='background-color: $bgcolor'>";
    $end_tag = '</span>';

    if(is_array($search_criteria))
    {
        foreach($search_criteria as $term)
        {
            $highlighted_results = $start_tag . $term . $end_tag;

            $search_results = eregi_replace($term, $highlighted_results, $search_results);
        }
    }

    return $search_results;
}

$term[0] = 'John Doe';
$term[1] = 'Jane Smith';
$term[2] = 'Bill Bobbins';

$db_result_text = "The users with the most points in this game were John Doe, Jane Smith, and Bill Bobbins.";

$result_text = highlight_search_criteria($db_result_text, $term);

//result_text is the sentence or string with a highlighted word
echo " result_text =     ", $result_text;

?>

Link to comment
Share on other sites

You were overwriting your returned string every time. You need to check which time it is that you're looping through and use the previously created string (with new background colors added) as the string to search through for the second term. Hope this helps.

 

<?php
$string = "<p>The users with the most points in this game were John Doe, Jane Smith, and Bill Bobbins.</p>";
$term = array("John Doe", "Jane Smith", "Bill Bobbins");
$bgcolor = "yellow";
foreach($term as $value) {
$count++;
if(!empty($value)) {
	$replaceWith = "<span style=\"background-color:$bgcolor;\">" . $value . "</span>";
	if($count==1) {
		$result = eregi_replace($value, $replaceWith, $string);
	} else {
		$result = eregi_replace($value, $replaceWith, $result);
	}
}
}

echo $result;
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.