Code:
<?php
$pat = '/hello (h)ow are y(o)u/';
$haystack = 'hello how are you doing today?';
preg_match_all($pat, $haystack, $out);
print_r($out);
?>
outputs:
Code:
Array
(
[0] => Array
(
[0] => hello how are you
)
[1] => Array
(
[0] => h
)
[2] => Array
(
[0] => o
)
)
I want to output the original haystack and highlight the full pattern match red and highlight the substring pattern matches green, so my output should be this:
<font color="red">hello <font color="green">h</font>ow are y<font color="green">o</font>u</font> doing today?
How can I do this in php code? This has been boggling me for a while.
Note: preg_replace() replaces the full pattern not the substrings
Thank you