note 35838 added to function.highlight-string
| From: | Tommaso at rn2 dot php dot net | Date: | Tue, 16 Sep 2003 15:12:26 +0000 |
| Subject: | note 35838 added to function.highlight-string | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-56577@lists.php.net to get a copy of this message | ||
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/
----
Manual Page -- http://www.php.net/manual/en/function.highlight-string.php
Edit Note -- http://master.php.net/manage/user-notes.php?action=edit+35838
Delete Note -- http://master.php.net/manage/user-notes.php?action=delete+35838&report=yes
Reject Note -- http://master.php.net/manage/user-notes.php?action=reject+35838&report=yes