So I decided to have a stab at making a text highlighting system. At the moment, I'm just using str_replace
to replace a word (e.g. $rstr = str_replace("Console", "<c>Console</c>", $str
where $str
is the input string.
Something that stumped me was how to replace content inside of speech marks (") and quotes ('). For example, if the string "Console" turned into Console.WriteLine("Words");
, how would I replace "Words"
with <sr>"Words"</sr>
(<sr>
is defined in an external stylesheet)?
I had a though that I could use regex, but 1. I don't know how to write regex, and 2. I don't know how to use regex with str_replace
.
My workaround solution:
function hlStr($original)
{
$rstr = explode('"', $original);
return $rstr[0].'<sr>"'.$rstr[1].'"</sr>'.$rstr[2];
}