IMC!


Contents


Photos

 







Browsing around...
News  News Links  Links Blog  Blog Italiano  Español 
Show code in a web page

Maybe you want to show some programming code in a web page. There are some problems about the special characters (or escape sequences), which can give problems for the page rendering or be shown badly. To avoid these problems, PHP gives us the functions highlight_file(filename), which reads the code from a file and visualizes it, and highlight_string(string), which reads code from a string and visualizes it. Both functions substitute the HTML special characters with the right entities, in order to format correctly the code for the web. If the code is PHP, the functions colours the commands (as in the advanced programming editors). Here you can find two PHP functions to show without problems any kind of programming code.

Code of show_code1.php
<?PHP
function show_code($path)
{
# Shows file name
$name=substr(strrchr($path'/'),1);
if (
$name
{
  echo 
'<DIV style="font-family: Verdana; font-size: 12px;" align="left"><B>Code of ' $name '</B></DIV>';
}
else
{
  echo 
'<DIV style="font-family: Verdana; font-size: 12px;" align="left"><B>Code of ' $path '</B></DIV>';
}
#Shows content
echo '<DIV style="padding: 5px; border: dashed 1px #99c; background-color: #eef; width:570px; overflow: auto;">';
highlight_file($path,0);
echo 
'</DIV>';
}

show_code('show_code1.php');
?>


Sometimes the function highlight_file() is disabled for secutiry reasons. In this situation, you have to open the file with the code, read the code to a string, show the string with highlight_string(), which usually is not disabled.This is the code:

Code of show_code2.php
<?PHP
function show_code($path)
{
# Shows file name
$name=substr(strrchr($path'/'),1);
if (
$name
{
  echo 
'<DIV style="font-family: Verdana; font-size: 12px;" align="left"><B>Code of ' $name '</B></DIV>';
}
else
{
  echo 
'<DIV style="font-family: Verdana; font-size: 12px;" align="left"><B>Code of ' $path '</B></DIV>';
}
#Shows content
echo '<DIV style="padding: 5px; border: dashed 1px #99c; background-color: #eef; width:570px; overflow: auto;">';
$codice=file_get_contents($path);
highlight_string($codice,0);
echo 
'</DIV>';
}

show_code('show_code2.php');
?>






Fatal error: Call to undefined function sqlite_open() in /membri/giacobbe85/include/commenti.inc.php on line 324