PHP/CSS find word in string, change its color for display. Having a problem, can’t find its solution, any suggestions? thanks.
<pre>
<?php
$str="Try to realize the truth... there is no spoon."; // spoon can be anywhere in string
$array = explode(" ", $str);
for($i=0;$i < count($array);$i++)
{
if ($array[$i] == "spoon") {
?><span style="color:red;"><?php echo echo $array[$i]." "; ?></span>
<?php
} else {
echo $array[$i]." ";
}
} ?>
</pre
I would personally use:
Note: str_ireplace is the Case-insensitive version str_replace.
Also… obviously you will need to define the css for ‘hilight’ somewhere!