I'm trying to highlight text changes in color in a paragraph. Basically, I am comparing one string of words with another string and I want to change the color of the font in the second string to show what changes have been added. The coding I am using works for the most part, but there are still some errors in it. Certain words are not highlighted completely when they should be and for no apparent reason one particular letter will have the font color changed throughout the string for no reason. Can anybody see where I am going wrong with the supplied text? I gave a comparison of two strings. Thanks in advance.
$old_string = "abc d e f g hafka hhd nn";
$new_string = "abc d e f g new change hafka in string hhd nn";
$old2 = explode(' ', $old_string);
$new2 = explode(' ', $new_string);
// What's different in $new?
$differences = (array_diff($new2, $old2));
foreach($differences as $word)
{
$replacement = "<font color=#00FF00><b>".$word."</b></font>";
$new_string = str_replace($word, $replacement, ($new_string));
}
echo "<br><br>";
echo "$new_string";