note 35838 deleted from function.highlight-string by sniper
| From: | sniper@php.net | Date: | Thu, 02 Oct 2003 14:24:55 +0000 |
| Subject: | note 35838 deleted from function.highlight-string by sniper | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-57614@lists.php.net to get a copy of this message | ||
Note Submitter: Tommaso D'Argenio
----
someone has posted a function to highlight code between
[source] and [/source], this is my version:
function color_source($string,$strip_php_tag=false)
{
$array_contenido = explode("[source]",$string);
$final = $array_contenido[0];
for($i = 1;$i <= count($array_contenido);$i++)
{
$array_contents = explode("[/source]",$array_contenido[$i]);
if(!stristr($array_contents[0],"<?php"))
$array_contents[0]=highlight_string(
"<?php".
$array_contents[0].
"?>",1);
else
$array_contents[0]=highlight_string($array_contents[0],1);
if($strip_php_tag)
{
$t = str_replace("<?php","",$array_contents[0]);
$t = str_replace("?>","",$t);
$array_contents[0] = $t;
}
$final .='
<span class="blogbody" style="font-size:80%">'.
$array_contents[0].
'</span>'.
$array_contents[1];
}
return $final;
}
example:
$string = '$a+=1;';
echo color_source($string,true);
as you can see the highlighting work even if
the open tag "<?php" and "?>" doesn't exist in
the string to process.
you can specify if need to strip or not the php tag
"<?php" and "?>". The function automatically add the open
tag for get the highlight_string() function work in right way.
the function above use the ability of highlight_string()
function to output the result direct to a string in this way
you've not to use the ob_* functions.
NOTE: you don't have to escape ' or " because i use the
addslashes() function on the string before highlight it.
enjoy the result.
Cheers,
Tommaso D'Argenio
infoNOSPAM@holosoft.it
http://www.holosoft.it/
http://lafucina.holosoft.it/kronoclass/