php fputcsv 和封闭字段

发布于 2024-08-26 16:40:15 字数 955 浏览 3 评论 0原文

我正要问与这里提出的问题相同的问题... 强制 fputcsv 对 *所有* 字段使用附件

问题是

当我使用 fputcsv 写出一行时 PHP 将添加到打开的文件句柄 任意列的封闭字符 它相信需要它,但会 保留其他列不带 外壳。

例如,您最终可能会得到 像这样的线

11,“鲍勃”,詹金斯,“200 main st. USA “,等等

缺少附加虚假空格 每个字段的末尾,有没有 强制 fputcsv 始终包含的方法 带有外壳的列(默认 到一个“)字符?

答案是:

不,fputcsv() 仅包含该字段 满足以下条件

/* enclose a field that contains a delimiter, an enclosure character, or a newline */
if (FPUTCSV_FLD_CHK(delimiter) ||
  FPUTCSV_FLD_CHK(enclosure) ||
  FPUTCSV_FLD_CHK(escape_char) ||
  FPUTCSV_FLD_CHK('\n') ||
  FPUTCSV_FLD_CHK('\r') ||
  FPUTCSV_FLD_CHK('\t') ||
  FPUTCSV_FLD_CHK(' ')
)

没有“始终包含”选项。

我需要创建一个 CSV 文件,将每个字段包含在内......什么是最好的解决方案?

提前致谢...

I was just about to ask the same questions as the question aksed here.... Forcing fputcsv to Use Enclosure For *all* Fields

The question was

When I use fputcsv to write out a line
to an open file handle, PHP will add
an enclosing character to any column
that it believes needs it, but will
leave other columns without the
enclosures.

For example, you might end up with a
line like this

11,"Bob ",Jenkins,"200 main st. USA
",etc

Short of appending a bogus space to
the end of every field, is there any
way to force fputcsv to always enclose
columns with the enclosure (defaults
to a ") character?

The answer was:

No, fputcsv() only encloses the field
under the following conditions

/* enclose a field that contains a delimiter, an enclosure character, or a newline */
if (FPUTCSV_FLD_CHK(delimiter) ||
  FPUTCSV_FLD_CHK(enclosure) ||
  FPUTCSV_FLD_CHK(escape_char) ||
  FPUTCSV_FLD_CHK('\n') ||
  FPUTCSV_FLD_CHK('\r') ||
  FPUTCSV_FLD_CHK('\t') ||
  FPUTCSV_FLD_CHK(' ')
)

There is no "always enclose" option.

I need to create a CSV file will every field enclosed... What would be the best solution?

Thanks in advance...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

廻憶裏菂餘溫 2024-09-02 16:40:15

推出你自己的函数 - 这并不难:(

 function dumbcsv($file_handle, $data_array, $enclosure, $field_sep, $record_sep)
 {
     dumbescape(false, $enclosure);
     $data_array=array_map('dumbescape',$data_array);
     return fputs($file_handle, 
         $enclosure 
         . implode($enclosure . $field_sep . $enclosure, $data_array)
         . $enclosure . $record_sep);
 }
 function dumbescape($in, $enclosure=false)
 {
    static $enc;
    if ($enclosure===false) {
        return str_replace($enc, '\\' . $enc, $in);
    }
    $enc=$enclosure;
 }

上面是使用 unix 风格转义)

C.

Roll your own function - its not hard:

 function dumbcsv($file_handle, $data_array, $enclosure, $field_sep, $record_sep)
 {
     dumbescape(false, $enclosure);
     $data_array=array_map('dumbescape',$data_array);
     return fputs($file_handle, 
         $enclosure 
         . implode($enclosure . $field_sep . $enclosure, $data_array)
         . $enclosure . $record_sep);
 }
 function dumbescape($in, $enclosure=false)
 {
    static $enc;
    if ($enclosure===false) {
        return str_replace($enc, '\\' . $enc, $in);
    }
    $enc=$enclosure;
 }

(above is using unix style escaping)

C.

断桥再见 2024-09-02 16:40:15

解决方法:假设您的数据位于二维数组中,您可以附加一个强制引用的字符串,并且您确定该字符串不包含在您的数据中(此处为“#@ @#”),然后将其删除:

    $fp = fopen($filename, 'w');
    foreach ($data as $line => $row) {
        foreach ($row as $key => $value) {
            $row[$key] = $value."#@ @#";
        }           
        fputcsv($fp, $row);
    }

    fclose($fp);
    $contents = file_get_contents($filename);
    $contents = str_replace("#@ @#", "", $contents);
    file_put_contents($filename, $contents);

A workaround: Supposing you have your data in a 2-dimensional array, you can append a string that will force quoting and you are sure is not contained in your data ("#@ @#" here) and then remove it:

    $fp = fopen($filename, 'w');
    foreach ($data as $line => $row) {
        foreach ($row as $key => $value) {
            $row[$key] = $value."#@ @#";
        }           
        fputcsv($fp, $row);
    }

    fclose($fp);
    $contents = file_get_contents($filename);
    $contents = str_replace("#@ @#", "", $contents);
    file_put_contents($filename, $contents);
情场扛把子 2024-09-02 16:40:15

我也遇到了同样的问题,我已经解决了,如下。
我已在每个数组值中插入单引号。
这样当你用excel打开csv文件时,就不会再显示科学计数法E+15了。

在这里我和大家分享一下我的做法。

这对我有用,我希望你也能这么做。
问候

        // this function add a single quote for each member of array
         function insertquote($value) {
            return "'$value'";
        }

        # here i send each value of array 
        # to a insertquote function, and returns an array with new values, 
        # with the single quote.

        foreach ($list as $ferow) {
            fputcsv($fp, array_map(insertquote, $ferrow), ';');
        }

I have encountered the same problem, and I have solved it as follows.
I have inserted single quotes to each array value.
This way when you open the csv file with excel, the scientific notation E + 15 will no longer be displayed.

Here I share you as I did.

This worked for me, I hope you do too.
Regards

        // this function add a single quote for each member of array
         function insertquote($value) {
            return "'$value'";
        }

        # here i send each value of array 
        # to a insertquote function, and returns an array with new values, 
        # with the single quote.

        foreach ($list as $ferow) {
            fputcsv($fp, array_map(insertquote, $ferrow), ';');
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文