Text output with line numbering

Submitted by:David Villa

Date added:13 February, 2013

Category:PHP

Place line numbers in text output format using CSS and PHP

Tags: line numbering

Code Snippet:

<style title="text/css">
.numbers{
float:left;
padding-right:8px;
background-color:#CCCCCC;
}
.text{
float:left;
margin-left:2px;
width:500px;
}
.codingLine1{
padding-left:2px;
padding-right:2px;
display:block;
background-color:#EEEEEE;
}
.codingLine2{
padding-left:2px;
padding-right:2px;
display:block;
background-color:#E2E2E2;
}
</style>

<?php
function lineCount($code){
$lines = explode("\n",htmlentities($code));
$i = 1;
echo'<div class=numbers>';
foreach($lines as $line){
echo $i.'. <br />';
$i++;
}
echo '</div>';
$row_count = 0;
echo'<div class=text>';
foreach($lines as $line){
$color = ($row_count % 2) ? 'codingLine1' : 'codingLine2';
echo '<span class='.$color.'>'.$line.'</span>';
$row_count++;
}
echo '</div>';
}

$code = "This
Is
My
Code:
<?php
echo 'PHP is good';
?> ";

lineCount($code);
?>
 
 

Comments