PHP code highlight

February 27, 2008 | PHP

This script will highlight string inside [code] and [/code] only.

< ?php
// function to call
function highlight_code($txt){
	$hasil = preg_replace_callback('{[code]((.|n)+?)[/code]}i',"replace_code", $txt);
	return $hasil;
}

// main function
function replace_code($ketemu){
	 $hasil = trim($ketemu[1], "n ");
	 return highlight_string($hasil, true);
}

// sample of usage:
$string = "It's PHP info: [ code ]  [ /code ]";
echo highlight_code($string);
?>

The main function is the replace_code() function that will highlight the string. But we need to return the the highlighted string back into the full strings. So we use the callback, using highlight_code() function.

Good luck ^_^

No comment

This post has no comments, you can be the first!

 

Leave a comment

Allowed tags are: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>