加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

如何从PHP中的csv文件中提取数据

发布时间:2020-12-13 16:26:51 所属栏目:PHP教程 来源:网络整理
导读:我有一个csv文件,看起来像这样 $lines[0] = "text,with commas","another text",123,"text",5;$lines[1] = "some without commas","text";$lines[2] = "some text with commas or no","text"; 我想要一张桌子: $t[0] = array("text,"123","5");$t[1] = arra
我有一个csv文件,看起来像这样
$lines[0] = "text,with commas","another text",123,"text",5;
$lines[1] = "some without commas","text";
$lines[2] = "some text with commas or no","text";

我想要一张桌子:

$t[0] = array("text,"123","5");
$t[1] = array("some without commas","text");
$t[2] = array("some text,with comma,s or no",NULL,"text");

如果我使用split($lines [0],“,”)我会得到“文本”,“用逗号”…
有什么优雅的方式吗?

您可以使用 fgetcsv来解析CSV文件,而无需担心自己解析.

PHP示例手册:

$row = 1;
if (($handle = fopen("test.csv","r")) !== FALSE) {
    while (($data = fgetcsv($handle,1000,",")) !== FALSE) {
        $num = count($data);
        echo "<p> $num fields in line $row: <br /></p>n";
        $row++;
        for ($c=0; $c < $num; $c++) {
            echo $data[$c] . "<br />n";
        }
    }
    fclose($handle);
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读