note 86751 deleted from function.fgets by danbrown

From: Date: Sun, 02 Nov 2008 15:18:01 +0000
Subject: note 86751 deleted from function.fgets by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-146051@lists.php.net to get a copy of this message
Note Submitter: crowsjunk at gmail dot com ---- I was beating my head against the wall trying to find some way to parse a file with a lot of variance in lines (Name=Value) or (Name=Value Value Value Value Value) with a big mix of spaces, line feeds, carriage returns, and new lines... the code below generates a page, constructed with a table, showing each character of each line in one row and in the next row the ascii value of that character. I have it color the background of the control and delimit characters (different colors for each that I will encounter...) I hope someone finds this useful... <?php // Naming convention: $a[#] = #; // [#] is the element of the array // # is the delimiter character number in ASCII $a[0] = 9; $a[1] = 10; $a[2] = 13; $a[3] = 61; // Naming convention: td.dc# // # is the delimiter character number in ASCII ?> <head> <style type="text/css"> <!-- td.dc9 { background: #99FF99; } td.dc10 { background: #6699FF; } td.dc13 { background: #FF99FF; } td.dc61 { background: #FFCC33; } td.e_space { background: #FFFFCC; } td.h_row { background: #C0C0C0; } <!-- </STYLE> <? // The value below for $file_num_lines = 25; // determines how many lines will be read $file_num_lines = 25; $ts = 0; $file_name_ts = @fopen(' ',"r") or die ( "no such file!!!!" ); $file_line_ta = fgets($file_name_ts); while ($x < $file_num_lines) { if ($ts < strlen($file_line_ta)) { $ts = strlen($file_line_ta); } $file_line_ta = fgets($file_name_ts); $x++; } $t_width = round((100/$ts), 1)."%"; fclose($file_name_ts); $file_name = @fopen(' ',"r") or die ( "no such file!!!!" ); $file_line = fgets($file_name); $i = 0; echo "<TABLE border = '1' width = 100%><TR>"; while ( $i < $ts ){ echo ("<TD class = h_row align = center width =".$t_width.">".$i."</TD>"); $i++; } echo "</TR>"; $i = 0; while( ($i < $file_num_lines) and (! feof($file_name)) ) { echo "<TR border = 1>"; while($j < strlen($file_line)) { $df = 0; while ($m < count($a)) { if(ord(substr($file_line, $j, 1)) == $a[$m]) { if( $a[$m] > 32) { echo "<TD class = dc".$a[$m]." align = 'center' width =".$t_width.">".substr($file_line, $j, 1)."</TD>"; $df = 1; $m = count($a)+1; } else { echo "<TD class = dc".$a[$m]." align = 'center' width =".$t_width.">&nbsp;</TD>"; $df = 1; $m = count($a)+1; } } if ($df == 0 and $m >= (count($a) - 1)) { echo "<TD align = 'center' width =".$t_width.">".substr($file_line, $j, 1)."</TD>"; } $m++; } $m = 0; $j++; } $y = 0; while ($y < ($ts - $j)) { echo "<TD align = 'center' class = e_space width =".$t_width.">&nbsp;</TD>"; $y++; } $j = 0; echo "</TR>"; echo "<TR border = 1>"; while($j < strlen($file_line)) { $df = 0; $m = 0; while ($m < count($a)) { if(ord(substr($file_line, $j, 1)) == $a[$m]) { echo "<TD align = 'center' class = dc".$a[$m]." width =".$t_width.">".ord(substr($file_line, $j, 1))."</TD>"; $df = 1; $m = count($a)+1; } if ($df == 0 and $m >= (count($a) - 1)) { echo "<TD align = 'center' width =".$t_width.">".ord(substr($file_line, $j, 1))."</TD>"; } $m++; } $j++; } $y = 0; while ($y < ($ts - $j)) { echo "<TD align = 'center' class = e_space width =".$t_width.">&nbsp;</TD>"; $y++; } echo "</TR>"; $file_line = fgets($file_name); $i++; $j = 0; $k = 0; $m = 0; } echo "</TABLE>"; fclose($file_name); ?>

« previous php.notes (#146051) next »