SplFileObject returns an empty line at the end [message #171737] |
Wed, 19 January 2011 22:01 |
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
|
|
|