...

View Full Version : null line problem



zodehala
10-27-2009, 11:26 PM
<?php
$handle = @fopen("sql.txt", "r");
if ($handle) {
$i=1;
while (!feof($handle)) {
$buffer = fgetss($handle, 4096);
echo $i." - ".$buffer."<br/>";
$i++;
}
fclose($handle);
}
?>



it read file like following
why it assing number as it is null (no line) ?


1 - SHOW AUTHORS
2 -
3 - SHOW BINARY LOGS
4 -
5 - SHOW BINLOG EVENTS
6 -
7 - SHOW CHARACTER SET
8 -
9 - SHOW COLLATION
10 -
11 - SHOW COLUMNS
12 - SHOW CONTRIBUTORS
13 - SHOW CREATE DATABASE
14 - SHOW CREATE EVENT
15 - SHOW CREATE FUNCTION
16 - SHOW CREATE PROCEDURE
17 - SHOW CREATE TABLE
18 - SHOW CREATE TRIGGER
19 - SHOW CREATE VIEW
20 - SHOW DATABASES
21 - SHOW ENGINE
22 - SHOW ENGINES
23 - SHOW ERRORS
24 - SHOW EVENTS

kbluhm
10-27-2009, 11:29 PM
Are those lines blank in sql.txt? You're echoing the line number, hyphen, and data with no check if the line is empty.

zodehala
10-28-2009, 12:34 AM
Are those lines blank in sql.txt? You're echoing the line number, hyphen, and data with no check if the line is empty.

yes they are blank in sql.txt. how i can block to show in page ?

oesxyl
10-28-2009, 12:45 AM
yes they are blank in sql.txt. how i can block to show in page ?



function notempty($a){
return ($a !== '');
}

$content = file("sql.txt");
$newcontent = array_filter($content,"notempty");

because that lines could be just \n or could contain spaces you must change notempty function to do what you need.

best regards

kbluhm
10-28-2009, 12:58 AM
$handle = fopen( './sql.txt', 'r' );

if ( FALSE !== $handle )
{
$i = 1;
while ( FALSE !== ( $buffer = fgetss( $handle ) ) )
{
// only display non-empty lines
if ( '' !== trim( $buffer ) )
{
echo $i++, ' - ', $buffer, "<br />\n";
}
}
fclose( $handle );
}


Or suck them all in using file() and automatically avoid the crap. These flags are only available since PHP 5, which you should be using by now anyhow. :)


$lines = file( './sql.txt', FILE_SKIP_EMPTY_LINES | FILE_IGNORE_NEW_LINES );

foreach ( $lines as $i => $line )
{
echo $i + 1, ' - ', $line, "<br />\n";
}

zodehala
10-28-2009, 02:20 AM
$handle = fopen( './sql.txt', 'r' );

if ( FALSE !== $handle )
{
$i = 1;
while ( FALSE !== ( $buffer = fgetss( $handle ) ) )
{
// only display non-empty lines
if ( '' !== trim( $buffer ) )
{
echo $i++, ' - ', $buffer, "<br />\n";
}
}
fclose( $handle );
}



first code is ok



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum