Posts Tagged ‘highlight’

Highlight String in PHP

Posted 23 Jan 2010 — by admin
Category php

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

Highlighted PHP.Net

Highlighted PHP.Net

***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.