Highlight String in 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.

Related posts:

  1. Absolutize Relative Links Using PHP and Preg_Replace_Callback
This entry was posted in php and tagged , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

One Comment

  1. Posted February 5, 2010 at 2:01 am | Permalink

    Very good article I enjoy your site keep up the amazing blog posts

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>