2013-05-10 116 views
1

嗨进出口试图使一个函数,它在一个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'); 
} 
+4

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

+1

http://stackoverflow.com/questions/2805427/how提取数据从csv-file-in-php记住找一个已经问过同样问题的人! – 2013-05-10 12:29:16

+0

我知道有很多问题,但没有我需要的答案。在CSV文件上的PHP函数可以正常工作,但在服务器上不正确。那为什么我要求正则表达式的方式,因为这是一个解决方法,直到服务器问题得到解决。 – MasterT 2013-05-10 12:37:26

回答

1

,我认为你的答案是在这里:

exploding a string using a regular expression

由于@Casimir等伊波利特在页面说:

你可以做的工作使用preg_match_all

$string="a,b,c,(d,e,f),g,'h, i j.',k"; 

preg_match_all("~'[^']++'|\([^)]++\)|[^,]++~", $string,$result); 
print_r($result[0]); 

说明:

诀窍是之前匹配括号,

~   Pattern delimiter 
' 
[^']  All charaters but not a single quote 
++   one or more time in [possessive][1] mode 
' 
|   or 
\([^)]++\) the same with parenthesis 
|   or 
[^,]  All characters but not a comma 
++ 
~ 

,如果你有一个以上的分隔符喜欢的报价(即用于打开和关闭一样),你可以写你的模式是这样,用捕获组:

$string="a,b,c,(d,e,f),g,'h, i j.',k,°l,m°,#o,p#,@q,[email protected],s"; 

preg_match_all("~(['#@°]).*?\1|\([^)]++\)|[^,]++~", $string,$result); 
print_r($result[0]); 

解释:

(['#@°]) one character in the class is captured in group 1 
.*?  any character zero or more time in lazy mode 
\1   group 1 content 

随着嵌套父分析:

$string="a,b,(c,(d,(e),f),t),g,'h, i j.',k,°l,m°,#o,p#,@q,[email protected],s"; 

preg_match_all("~(['#@°]).*?\1|(\((?>[^()]++|(?-1)?)*\))|[^,]++~", $string,$result); 
print_r($result[0]); 
+0

谢谢你,这正是我需要的工作,一种享受,谢谢你,我的朋友 – MasterT 2013-05-12 20:38:49

+0

什么是空白的像字段1 ,,,字段4 – MasterT 2013-06-13 19:17:01

0

可以使用使preg_split与PREG_SPLIT_DELIM_CAPTURE选项。

$海峡=字段1,字段2,字段3,字段4, “一些文字,其他一些文字” 栏位6;

那么像这样

$match = preg_split("ypir expression", $str, null, PREG_SPLIT_DELIM_CAPTURE); 
+3

更好的选择是使用内置的CSV处理函数 – 2013-05-10 12:30:36

+0

@MarkBaker内置的CSV函数无法通过换行符正确处理CSV。 – Gerry 2013-05-10 12:34:44

+1

@Gerry - 那么这个答案是如何处理换行符的,或者OP在他的问题中提到了什么? – 2013-05-10 12:36:19

0

我不会回答这个问题被问,因为他要求的错误解决他的问题的问题。但是,我希望这个解决方案会更适合他:

在问题的代码来看,在OP基本上读通过PHP CSV文件并将其导入MySQL数据库。

的MySQL实际上提供了直接做这与它的LOAD DATA INFILE语法的方法,而不必解析文件中的PHP的。它比通过PHP处理它快得多,并且完全避免了OP所具有的全部问题。

在PHP中,你只需要做到以下几点:

$query = <<<eof 
    LOAD DATA INFILE {$filename} INTO {$table} 
    FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' 
    LINES TERMINATED BY '\r\n' 
    IGNORE 1 LINES 
    field1, field2, field3, field4, field5, field6 
eof; 
mysqli_query($conn, $query); 

您可能需要修改查询了一下代码中的一些更复杂的东西(即转换日期格式,等等),但一旦你掌握了LOAD DATA INFILE的语法,你会发现它的合并相当简单。

我希望有帮助。

+0

好吧,只是为了清理一些东西,因为你现在所有的事情。即时通讯这样做,以便操作数据在手之前更容易,即时通讯正则表达式的方式,因为有一些文本格式和文字替换也正在使用。我粘贴的功能只是它的基本版本,所以其他人可以根据需要使用它。如果任何人都可以告诉我正则表达式,那将会很棒。 – MasterT 2013-05-10 13:39:30

相关问题