PHP

PHP code highlight

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 ^_^

Leave a Reply