Written by Sean Behan on Sun Jun 17th 2012

Function for highlighting text/strings in PHP.

$content = file_get_contents("http://php.net/");

print highlight("PHP", $content);

function highlight($match, $string){ return str_ireplace($match, "<span style='background:yellow'>$match</span>", $string); }

Will output something like this [caption id="" align="alignnone" width="345" caption="Highlighted PHP.Net"]Highlighted PHP.Net[/caption]

***Notice all the styles are gone :( ... would need to parse the document body to maintain stylesheet and other 'php' strings that might be in resources paths.


Tagged with..
#file_get_contents #highlight #php #php

Just finishing up brewing up some fresh ground comments...