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
Return to the default flat view Create a new topic Submit Reply
SplFileObject returns an empty line at the end [message #171737] Wed, 19 January 2011 22:01 Go to previous message
Mladen Gogala is currently offline  Mladen Gogala
Messages: 13
Registered: December 2010
Karma:
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
[Message index]
 
Read Message
Read Message
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: Sat Nov 23 21:07:16 GMT 2024

Total time taken to generate the page: 0.06686 seconds