<?
function colorphp($var,$start=1){
$x=explode("<br />",highlight_string($var,true));
$v="";
for($i=0;$i<count($x);$i++){
$v.="
<font face='verdana' size='1' color='#000000'><strong>".$start.":</strong></font> ".$x[$i]."
<br />";
$start++;
}
$t='<table width="100%" cellpadding="2" cellspacing="0">';
$t.="
<tr valign='top'>";
$t.="
<td><span class="med">PHP:</span></td>";
$t.="
<tr valign='top'>";
$t.="
<td><p class='code'>{$v}</p></td>";
$t.="
</tr>
</table>";
return $t;
}
?>
<?
//1st method
$text="
testing
<?
echo 'hello';
?>
yep";
$colored=colorphp($text);
echo $colored;
/************
//Will ouput the following, but colored
1: testing
2: <?
3: echo 'hello';
4: ?>
5: yep
************/
//2nd method
$text="
testing
<?
echo 'hello';
?>
yep";
$colored=colorphp($text,3);
echo $colored;
/************
//Will ouput the following, but colored
3: testing
4: <?
5: echo 'hello';
6: ?>
7: yep
************/
//3rd method
$text="
testing
<?
echo 'hello';
?>
yep";
$colored=preg_replace('/<?(.*?)?>/ise',"colorphp('<?\1?>')",$text);
echo $colored;
/************
//Will ouput the following, but only the text on lines 2-4 would be colored.
1: testing
2: <?
3: echo 'hello';
4: ?>
5: yep
************/
?>
|