【发布时间】:2013-11-28 03:55:20
【问题描述】:
我正在使用以下代码使用 txt 文件将数据导入数据库
while (!feof($handle)) // Loop til end of file.
{
$buffer = fgets($handle, 4096); // Read a line.
$data_array =explode("|",$buffer);//Separate string by the means of |
$escaped_array = array_map('mysql_escape_string',$data_array);
$escaped_array = '("'.implode('","',$escaped_array).'") ';
if($i !=1) {
echo $sql = 'INSERT INTO '.$tablename.' VALUES'.$escaped_array;
mysql_query($sql) ; //or die(mysql_error().$tablename);// use mysql insert query here
//exit;
}
$i++;
} //end while
它插入像这样的值 INSERT INTO PROVIDER_DEMOGRAPHIC VALUES("10000104","Sand","David","C","MD","Family Practice","","N","N","N","P"," 125\r\r\n") INSERT INTO PROVIDER_DEMOGRAPHIC VALUES("10000105","Stucky","Mitchell","B","MD","家庭实践","","N","N", "N","P","101\r\r\n")
我不想在最后的字段中添加额外的字符,例如 \r\n\r 请告诉我如何修剪它。
【问题讨论】:
-
使用
fgetcsv并将分隔符设置为|而不是默认的,