php fputcsv和包含字段 [英] php fputcsv and enclosing fields

查看:430
本文介绍了php fputcsv和包含字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想问问题aksed在这里问题相同的问题.... 强制fputcsv使用机箱对于* all *字段



问题是



< blockquote>

当我使用fputcsv写一行
到一个打开的文件句柄,PHP会添加
一个包围字符到它认为需要它的任何列
,但会
离开其他列没有
机箱。



例如,你可能会得到一个
行, / p>

11,Bob,Jenkins,200 main st。USA
等。



没有在每个字段的末尾附加一个伪造的空格
,是否有任何
方法强制fputcsv总是用enclosure(
到字符)包含
列?


答案是:


否,fputcsv()在以下条件下只包含字段




  *包含一个包含分隔符,封装字符或换行符的字段* / 
if(FPUTCSV_FLD_CHK(delimiter)||
FPUTCSV_FLD_CHK(enclosure)||
FPUTCSV_FLD_CHK(escape_char)||
FPUTCSV_FLD_CHK('\\\
')||
FPUTCSV_FLD_CHK('\r')||
FPUTCSV_FLD_CHK('\t')||
FPUTCSV_FLD_CHK('')




没有always enclose选项。


我需要创建一个CSV文件,最佳解决方案?



提前感谢...

解决方案

你自己的函数 - 它不难:

  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。


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...

解决方案

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.

这篇关于php fputcsv和包含字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆