用PHP读取制表符分隔的csv

我无法读取制表符分隔的csv文件(是的,我知道它的csv和没有tsv和 c用于制表符...)用php并将其分开。 当我使用echo或readfile输出我导入的数据时,所有制表符都被空格替换,我不能使用空格作为拼写器。</ p>

实际上我有点困惑这个 当我使用Google时问题不是很常见,所以也许我就是问题... </ p>

XAMPP v3.2.3和PHP 7.3.5版本存在问题</ p> \ n

  $ tempFile = fopen($ tempFilePath,“r”); 
$ uploadData = fread($ tempFile,filesize($ tempFilePath));
fclose($ tempFile);
\ n echo $ uploadData;

$ uploadData = str_replace('“','',$ uploadData);
$ uploadData = str_replace('
','
',$ uploadData);

$ uploadData = str_replace('','
',$ uploadData);
$ uploadData = str_replace(';',',',$ uploadData);
$ uploadData = str_replace('\ t ',',',$ uploadData); //在这里,我试图用冒号替换制表符,然后使用冒号
</ code> </ pre>
</ div>

展开原文

原文

I'm not able to read a tabulator seperarated csv file (and yes, i know its csv and no tsv and the c is for tabulator...) with php and seperate it right. When i give out my imported Data with echo or readfile all the tabulators are replaced by a space and i can't use space as a sperator.

Actually i'm a bit confused that this problem is not very common when i use Google, so maybe i'm the problem...

The Problem exists with XAMPP v3.2.3 and PHP Version 7.3.5

$tempFile = fopen($tempFilePath, "r");
        $uploadData = fread($tempFile, filesize($tempFilePath));
        fclose($tempFile);

        echo $uploadData;


        $uploadData =   str_replace('"','',$uploadData);    
        $uploadData = str_replace('
','
',$uploadData);
        $uploadData = str_replace('','
',$uploadData);
        $uploadData = str_replace(';',',',$uploadData);
        $uploadData = str_replace('\t',',',$uploadData); //Here i'm trying to replace the tabulator with a colon to work with it afterwards

2个回答



在PHP中读取CSV文件时,最好使用 fgetcsv 功能。 有了它,您可以指定文件的分隔符。 该函数将输出该行的一行。 你的分隔符应该是“\ t”</ code>。</ p>
</ div>

展开原文

原文

When reading a CSV file in PHP, it is best to use the fgetcsv function. With it you can specify the deliminator of the file. The function will output a row of the file. Your deliminator should be "\t".

drug95107
drug95107 看看这篇文章有一个非常类似的问题:链接
一年多之前 回复
doulvli9462
doulvli9462 问题实际上是更多的人正在使用它,我无法指定系统和配置。 所以我想使用冒号,分号和制表作为分隔符。 是的,我讨厌Excel这个...
一年多之前 回复



你应该使用 fgetcsv函数已经具有读取tsv的功能。</ p>

像这样:</ p>

  $  uploadData = []; 
if(($ handle = fopen(“test.csv”,“r”))!== false){
while(($ data = fgetcsv($ handle,0,“\ t”) ))!== false){
$ uploadData [] = $ data;
}
fclose($ handle);
}
</ code> </ pre>

如果 你想将tsv转换成csv你可以使用反函数 fputcsv : </ p>

  $ fh = fopen('file.csv'); 
foreach($ uploadData as $ datum){
fputcsv($ fh,$ datum);
}

nclclose($ fh);
</ code> </ pre>

如果要自动检测csv分隔符,可能这篇文章可以帮助你。</ p>
</ div>

展开原文

原文

You should use fgetcsv function which already has functionality to read tsv.

Like this:

$uploadData = [];
if (($handle = fopen("test.csv", "r")) !== false) {
    while (($data = fgetcsv($handle, 0, "\t")) !== false) {
        $uploadData[] = $data;
    }
    fclose($handle);
}

If you want to convert tsv to csv you can use inverse function fputcsv:

$fh = fopen('file.csv');
foreach ($uploadData as $datum) {
    fputcsv($fh, $datum);
}
fclose($fh);

If you want automatically detect csv delimiter maybe this article can help you.

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问