note 43680 deleted from function.highlight-string by tomsommer
| From: | tomsommer@php.net | Date: | Wed, 30 Jun 2004 07:20:11 +0000 |
| Subject: | note 43680 deleted from function.highlight-string by tomsommer | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-72159@lists.php.net to get a copy of this message | ||
Note Submitter: 2064 attttt earthlink ddddottt neeeeeet
----
Name: Alex R Austin
Ok, I was playing with the code given by people from posts below and the table didn't act nice
when window was resized. You could not understand what line number was of that line.
So, I decided to make a table where
[cell "number"][cell "code goes here"]
But I ran into a problem. When you break the highlighted string of code into lines, font color info
gets lost because continuation of the font tags in several places is severed. That is why
everyone's solution is to keep numbers in one cell and code in its whole chunk in another cell.
But manipulation with such a page would surely break line - code relationship. This code will not do
that. You can resize the page all you want, have longest lines of code and it will still be intact.
Here after a few hours of pain I made this function that would put back lost font color info when
lines are broken.
Here is my solution. The whole code with the CSS HTML page is included.
Just copy and paste.
PS: Change the file name in where is says ur_file.php to whatever your file is.
Code start --------------------->
<?
function fo($file)
{
if (strlen($file)!=0)
return fread(fopen($file,'r'), filesize($file));
}
function color_code($code)
{
$code = $copy = substr_replace(substr_replace
(highlight_string(stripslashes($code),true),'',0,28),'',-15,15);
do
{
$range=strchr($copy, '<font ');
$tagrange=substr($range,0,strlen($range)
-strlen(strchr($range, '</font>')));
$br=strchr($tagrange, '<br />');
if (strlen($br)>0)
{
$fulltag=substr($tagrange,0,
strlen($tagrange)-strlen(strchr($tagrange, '>')));
$l_len=strlen($code)-
strlen($range)+strlen($tagrange)+6;
$tag=$fulltag.'>';
}
while (strlen($br)>0)
{
$indexarray[]=$l_len-strlen($br);
$tagarray[]=$tag;
$br=strchr($br=substr($br,6), '<br />');
}
$copy=substr($range,1);
} while (strlen($copy)>0);
$max=count($tagarray);
for ($count=0;$count<$max;$count++)
{
$code=substr_replace($code,
$tagarray[$count],($indexarray[$count]+$offby),0);
$code=substr_replace($code,
'</font>',($indexarray[$count]+$offby-6),0);
$offby+=strlen($tagarray[$count])+7;
}
$max=count($code = split('<br />', $code));
for ($count=0;$count<$max;$count++)
{
$output.='<tr><td class="b">'
. ($count+1). '.</td><td
class="c">'.$code[$count].'</td></tr>';
}
return '<table border="0"
width="100%" cellspacing="0">'.$output.'</table>';
}
?>
<html>
<head>
<style type="text/css">
td, body {font: 11px Verdana,serif; font-weight:bold;}
table {border-width: 0px; width: 100%;
border-collapse:separate; border-spacing: 0px}
body, td.b {background-color: #3366FF; color: white;}
td.b {vertical-align:top; width: 20px;
text-align:right; white-space: nowrap;}
td.c {background-color: white; padding-left: 5px; color:#000000}
</style>
<title>Alex R Austin's code</title>
</head>
<body><br>
<? $file=fo('ur_file.php'); echo color_code($file); ?>
<br>
</body>
</html>