开发者

Escaping line break in csv php

I have a .csv template I'd wish for people to fill up, save it and upload it.

The problem is this, assuming some users would insert hidden line breaks in a row, when using fgetcsv() it would output the row broken by the hidden line breaks.

How can I escape the line break or sanitize my data?

Possible solution:

assume first row is correct, $count = count the number of delimiters until line break, the rebuild the text into an array as long as $count;

but i think the're better options available.

LATER EDIT

Here's the input *IMPORTANT[ ! ] : the data inside the excel file is "fine", it isn't broken, it's a single row!!! saving it as a csv file and opening it in notepad shows the following

asd;"asd
asd
asd";asd;asd

Here's the code

$handle = fopen("file.csv","r");
$data = fgetcsv($handle,开发者_高级运维";");
while($data = fgetcsv($handle)) {
    $array = explode(";",$data[0]);
    print_r($array);
}
fclose($handle);

Here's the echoed data

Array ( [0] => asd [1] => "asd ) Array ( [0] => asd ) Array ( [0] => asd" [1] => asd [2] => asd [3] => ) 

Thanks


it is very easy to test your case and see that there are no broken rows, if fields being properly quoted.

So, a CSV line like this

1,"joe
""Big Coyote""
Hopkins",598600

will be read with not a single problem.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜