嗨进出口试图使一个函数,它在一个CSV文件,然后将其插入文件whitch作品,但没有什么,我需要做的。PHP使preg_split为逗号而不是在双引号
在我使用爆炸摆脱文件行的时刻..
explode(",", $linearray);
这workes但如果有类似
field1,field2,field3,field4,"some text, some other text",field6
我得到这个数组
array(
[0]=>field1,
[1]=>field2,
[2]=>field3,
[3]=>field4,
[4]=>"some text,
[5]=>some other text",
[6]=>field6
)
这不是我想要的结果。我知道preg_split可以为我做,但我不擅长正则表达式。 我想要的结果是。
field1,field2,field3,field4,"some text, some other text",field6
array(
[0]=>field1,
[1]=>field2,
[2]=>field3,
[3]=>field4,
[4]=>some text, some other text,
[5]=>field6
)
请大家帮忙。从PHP类
功能CSV文件我已经写
$lineseparator = "\n";
$fieldseparator = "\n";
function ReadFile(){
$this->csvcontent = fread($this->_file,$this->size);
fclose($this->_file);
return ($this->csvcontent)? true : false ;
}
function InsertFileToSQL(){
$query = "";
$i_count = 0;
$queries = "";
$linearray = array();
$file_array = explode($this->lineseparator,$this->csvcontent);
$lines = count($file_array);
foreach($file_array as $key => $value) {
$value = trim($value," \t");
$value = str_replace("\r","",$value);
/***********************************************************************************************************
This line escapes the special character. remove it if entries are already escaped in the csv file
************************************************************************************************************/
$value = str_replace("'","\'",$value);
$value = str_replace("\"","",$value);
/***********************************************************************************************************/
$linearray = explode($this->fieldseparator,$value);
foreach($linearray as $key2 => $value2){
// Format all fields that match a date format the Reformat for SQL.
$date = explode("/", $value2);
if(count($date) == 3){
$linearray[$key2] = $date[2]."-".$date[1]."-".$date[0];
}
}
$linemysql = implode("','",$linearray);
if($linemysql != "" && $linemysql != NULL){
if($this->csvheader){
if($key != 0){
if($this->addauto == 1){
$query = "INSERT INTO `$this->db_table` VALUES (NULL,'$linemysql');";
}else{
$query = "INSERT INTO `$this->db_table` VALUES ('$linemysql');";
}
}else{
$lines--;
}
$insert = mysql_query($query) or die(mysql_error());
if($insert){
$queries .= $query . "\n";
$i_count++;
}
}else{
if($this->addauto == 1){
$query = "INSERT INTO `$this->db_table` VALUES (NULL,'$linemysql');";
}else{
$query = "INSERT INTO `$this->db_table` VALUES ('$linemysql');";
}
$insert = mysql_query($query) or die((mysql_error()." in QUERY: ".$query));
if($insert){
$queries .= $query . "\n";
$i_count++;
}
}
}else{
$this->null_row++;
$lines--;
}
}
if($this->save) {
$f = fopen($this->output_location.$this->outputfile, 'a+');
if ($f) {
@fputs($f, $queries);
@fclose($f);
}else{
echo("Error writing to the output file.", 'error');
}
}
$lines--;//fix array count
$text = "";
if($i_count - $this->null_row != 0){$i_count = $i_count - $this->null_row ;$text .= "<br>$i_count Records were inserted Successfully.";}
echo("Found a Total of $lines Record(s) in this csv file.<br>$this->null_row Record(s) were/are Blank or Null.$text", 'success');
}
str_getcsv()更适合这个 - http://www.php.net/manual/en/function.str-getcsv.php;或者如果您正在从CSV文件中读取数据,请使用fgetcsv() - http://www.php.net/manual/en/function.fgetcsv.php – 2013-05-10 12:28:24
http://stackoverflow.com/questions/2805427/how提取数据从csv-file-in-php记住找一个已经问过同样问题的人! – 2013-05-10 12:29:16
我知道有很多问题,但没有我需要的答案。在CSV文件上的PHP函数可以正常工作,但在服务器上不正确。那为什么我要求正则表达式的方式,因为这是一个解决方法,直到服务器问题得到解决。 – MasterT 2013-05-10 12:37:26