将光标移到/点击文章中的句子上,可以查看译文。      显示繁体中文内容    显示简体中文内容

How to keep backslash or change it to NULL in CSV file to generate SQL file in PHP
如何在CSV文件中保留反斜杠或者将它的改为空以生成SQL文件

0 0

I am generating SQL file from CSV file, but have some string files with slashes and the value is like :


"aaa","", 111, 222,"bbb","", 333


The value between""will be generated as one string


'", 111, 222,"bbb","', 333


But i hope to keep""as it should to be, or change it to NULL, like :


'NULL', 111, 222, 'bbb', 'NULL', 333


My code is like this :


while (($lineArr = fgetcsv($infile))!=FALSE){
foreach($lineArr as $line){
...
if ($line == '"')
$line = 'NULL';
...
}
}


This dose not work. i think $line =='"' is not correct, but i do not know how to process the whole part between"".

solution : i first change the array to string to process the '""', then change it back to array for further process.


while (($lineArr = fgetcsv($infile))!=FALSE){
$string = implode(",", $lineArr);
 if (strpos($string,',",')){
 $sKeepSlash = str_replace(',",',",NULL,", $string);
 $pLineArr = explode(',', $sKeepSlash);
 }
 foreach($lineArr as $line){
. . .
}
}


时间: 原作者:

0 0

Use fputcsv() to write, and fgetcsv() to read.

Found here : Escaping for CSV

原作者:
0 0

Find the position for the slashes first. for example, you can user array_keys.

this will return the positions of the markers. you can then use that to combine


$marker_positions = array_keys($array,"marker");
for ($position = $marker_positions[0]+1; $position <$marker_positions[0]; $position++) {
 $single_string. = $array[$position];
}


原作者:
...