FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » SplFileObject returns an empty line at the end
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
SplFileObject returns an empty line at the end [message #171737] Wed, 19 January 2011 22:01 Go to next message
Mladen Gogala is currently offline  Mladen Gogala
Messages: 13
Registered: December 2010
Karma: 0
Junior Member
In PHP 5.3, SplFileObject returns an empty line at the end of the file.
One of the cases that I have a problem with is the script below. The line
that reads "if (strlen(implode('',$row))==0) continue;" is offensive to
my sense of aesthetics. Is there any way to prevent objects of the
SplFileObject class from returning an empty line at the end of input?

#!/usr/bin/env php
<?php
if ($argc != 3) {
die("USAGE:script8.9 <table_name> <file name>\n");
}
$tname = $argv[1];
$fname = $argv[2];
$rownum = 0;

function create_insert_stmt($table, $ncols) {
$stmt = "insert into $table values(";
foreach(range(1,$ncols) as $i) {
$stmt.= ":$i,";
}
$stmt = preg_replace("/,$/", ')', $stmt);
return ($stmt);
}
try {
$db = new SQLite3("bookmarks.sqlite");
$res = $db->query("select * from $tname");
if ($db->lastErrorCode() != 0) {
throw new Exception($db->lastErrorMsg());
}
$ncols = $res->numColumns();
$res->finalize();
$ins = create_insert_stmt($tname, $ncols);
$res = $db->prepare($ins);
$fp=new SplFileObject($fname,"r");
while ($row = $fp->fgetcsv()) {
if (strlen(implode('',$row))==0) continue;
foreach(range(1,$ncols) as $i) {
$res->bindValue(":$i", $row[$i - 1]);
}
$res->execute();
if ($db->lastErrorCode() != 0) {
print_r($row);
throw new Exception($db->lastErrorMsg());
}
$rownum++;
}
print "$rownum rows inserted into $tname.\n";
}
catch(Exception $e) {
print "Exception:\n";
die($e->getMessage() . "\n");
}
?>


--
http://mgogala.byethost5.com
Re: SplFileObject returns an empty line at the end [message #171895 is a reply to message #171737] Thu, 20 January 2011 13:35 Go to previous message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 1/19/2011 5:01 PM, Mladen Gogala wrote:
> In PHP 5.3, SplFileObject returns an empty line at the end of the file.
> One of the cases that I have a problem with is the script below. The line
> that reads "if (strlen(implode('',$row))==0) continue;" is offensive to
> my sense of aesthetics. Is there any way to prevent objects of the
> SplFileObject class from returning an empty line at the end of input?
>
<code snipped>

Get rid of the empty line at the end of the file.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Learn PHP knowing C++
Next Topic: USA Free classified site for free ad posting !!!
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Wed Nov 27 07:15:55 GMT 2024

Total time taken to generate the page: 0.02525 seconds