Home php教程 php手册 php使用fputcsv()函数csv文件读写数据的方法

php使用fputcsv()函数csv文件读写数据的方法

Jun 06, 2016 pm 08:14 PM
csv file php function data Read and write

这篇文章主要介绍了php使用fputcsv()函数csv文件读写数据的方法,分析了fputcsv()函数针对csv文件的读写操作技巧,具有一定参考借鉴价值,需要的朋友可以参考下

本文实例讲述了php使用fputcsv()函数csv文件读写数据的方法。分享给大家供大家参考。具体分析如下:

fputcsv() 函数用于将数据格式为csv格式,以便写入文件或者数据库.

1.将字符串写入csv文件中,代码如下:

复制代码 代码如下:

$test_array = array(
    array("111","sdfsd","sdds","43344","rrrr"),
    array("sssssssss","gdfgfd","232323","wwewe","dsfds"),
    array("fgfg","e4343","dsfds","w2332","xcvxc"),
    array("11212","2323","344343","344343","rerreer"),
    array("fds","43344444","33333333","ttttttt","gggggggggggg"),
    array("kdfs","dsfdsfds","wewewe","sdsdddddddd","wwwwwwwwwww")
);
 
$file = fopen("test.csv","w") or die("Can't Open test.csv");
foreach($test_array as $line_array)
{
    $isSuccess = fputcsv($file,$line_array);
    print $isSuccess."
";
 if($isSuccess===false)
    {
        die("Can't write csv line".$line_array);
    }
}
fclose($file) or die("Can't close file test.csv.");


fputcsv()函数返回所写入行的字符的个数或者false,当写入失败时返回false.

2.将格式化的csv字符串保存到字符串中,代码如下:

复制代码 代码如下:

$test_array = array(
        array("111","sdfsd","sdds","43344","rrrr"),
        array("sssssssss","gdfgfd","232323","wwewe","dsfds"),
        array("fgfg","e4343","dsfds","w2332","xcvxc"),
        array("11212","2323","344343","344343","rerreer"),
        array("fds","43344444","33333333","ttttttt","gggggggggggg"),
        array("kdfs","dsfdsfds","wewewe","sdsdddddddd","wwwwwwwwwww")
);
ob_start();
$file = fopen("php://output","w") or die("Can't Open php://output");
foreach($test_array as $line_array)
{
        $isSuccess = fputcsv($file,$line_array);
        if($isSuccess===false)
        {
            die("Can't write csv line".$line_array);
        }
}

fclose($file) or die("Can't close file test.csv.");
$result = ob_get_contents();
ob_end_clean();


以用fgetcsv(file,length,separator,enclosure)函数读取csv文件.

fgetcsv的参数说明如下:

file:需要读取的csv文件,此参数是必需的。

length:表示大于csv文件中最长的行的长度的值。php5之前是必需参数。在php5中是可选参数,如果不设置此参数或者将其设为0,php将会读取.

一整行的数据。如果行的长度超过8192个字节时,应该将length值设定一个数,而不是让php自动去计算行的长度。

separator:指定数据的分隔符,,默认是逗号,如果指定为“;”,那么fgetcsv函数将按照“;”来解析行数据。

fgetcsv的返回值:

根据file的一行数据,返回一个数组,如果读取文件出错,则返回false,到达文件尾部时,也返回false.

下面是一个读取test.csv文件的例子:

复制代码 代码如下:

$file = fopen('test.csv','r') or die("Can't open file test.csv");
$color="#ff0000";
print '

';
while($csv_line=fgetcsv($file))
{
        print "";
        $len = count($csv_line);
        for($i=0;$i         {
            if($i%2==0)$color="#cccccc";
            else $color="#999999";
            print '';
        }
        print "";
}
print '
'.htmlentities($csv_line[$i]).'
';
fclose($file) or die("Can't close file test.csv!");

希望本文所述对大家的php程序设计有所帮助。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP Working with Database CakePHP Working with Database Sep 10, 2024 pm 05:25 PM

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

CakePHP Logging CakePHP Logging Sep 10, 2024 pm 05:26 PM

Logging in CakePHP is a very easy task. You just have to use one function. You can log errors, exceptions, user activities, action taken by users, for any background process like cronjob. Logging data in CakePHP is easy. The log() function is provide

See all articles