当前位置: 首页 > 网络学院 > 服务端脚本教程 > PHP > fgetcsv() 函数

PHP
php 无限分类的实现
常用PHP代码
windows下安装配置php视频教程
MySQL数据库结构和数据的导出和导入
PHP实现 IP Whois 查询
PHP5 this,self和parent关键字详解
PHP 安全技巧连载 #1[译]
PHP 安全技巧连载 #2[译]
PHP 安全技巧连载 #3[译]
PHP 安全技巧连载 #4[译]
PHP 安全技巧连载 #5[译]
PHP 安全技巧连载 #6[译]
PHP 安全技巧连载 #7[译]
PHP 安全技巧连载 #8[译]
PHP 安全技巧连载 #9[译]
PHP 安全技巧连载 #10[译]
PHP 安全技巧连载 #11[译]
PHP error_reporting的使用
PHP 安全技巧连载 #12
使用PHP做Linux/Unix守护进程

PHP 中的 fgetcsv() 函数


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-03-04   浏览: 533 ::
收藏到网摘: n/a

Definition and Usage
定义和用法

The fgetcsv() function parses a line from an open file, checking for CSV fields.
fgetcsv()函数的作用是:查找CSV域,从一个打开的文件中找出所有词段[每段用“,”隔开],然后以数组的形式输出它们。

The fgetcsv() function stops returning on a new line, at the specified length, or at EOF, whichever comes first.
fgetcsv()函数可以对是否输出新的一行词段作出设置,具体见下面的语法;当到达文档段落末尾[EOF]时,该函数将停止运行。

This function returns the CSV fields in an array on success, or FALSE on failure and EOF.
fgetcsv()函数将以数组的形式返回CSV域,如果失败或到达文档末尾[EOF]时,将返回False。

Syntax
语法

fgetcsv(file,length,separator,enclosure)

Parameter
参数
Description
描述
file Required. Specifies the file to check
必要参数。指定文件对象
length Optional. Specifies the maximum length of a line. Must be greater than the longest line (in characters) in the CSV file. Omitting this parameter (or setting it to 0) the line length is not limited, which is slightly slower.
可选参数。指定一个输出行的最大长度。CSV文件中的字符串长度必须要大于这个最大长度才可以被输出。如果不设置这个参数(或设置为0),表明长度是无限大,这将使得函数运行会变得比较缓慢

Note: This parameter is required in versions prior to PHP 5
注意点:这个参数仅在PHP 5之前版本中支持

separator Optional. A character that specifies the field separator. Default is comma ( , )
可选参数。指定域分隔符[field separator]。默认为“,”
enclosure Optional. A character that specifies the field enclosure character. Default is "
可选参数。指定用来包含字符的一个字符。默认是"


Tips and Notes
提示和注意点

Tip: Also see the fputcsv() function.
提示:可以回顾一下fputcsv()函数。


Example 1
案例1

<?php
$file = fopen("contacts.csv","r");
print_r(fgetcsv($file));
fclose($file);
?> 

The CSV file:
CSV文件

Kai Jim, Refsnes, Stavanger, Norway
Hege, Refsnes, Stavanger, Norway

The output of the code above will be:
上述代码将输出下面的结果:

Array
(
[0] => Kai Jim
[1] => Refsnes
[2] => Stavanger
[3] => Norway
)


Example 2
案例2

<?php
$file = fopen("contacts.csv","r");
while(! feof($file)) { print_r(fgetcsv($file)); }
fclose($file);
?> 

The CSV file:
CSV文件

Kai Jim, Refsnes, Stavanger, Norway
Hege, Refsnes, Stavanger, Norway

The output of the code above will be:
上述代码将输出下面的结果:

Array
(
[0] => Kai Jim
[1] => Refsnes
[2] => Stavanger
[3] => Norway
)
Array
(
[0] => Hege
[1] => Refsnes
[2] => Stavanger
[3] => Norway
)

评论 (0) All

登陆 | 还没注册?