如何在 PHP 中解析列中具有多行数据的 csv

发布于 2024-10-13 08:45:11 字数 372 浏览 3 评论 0原文

考虑像这样的 CSV 文件,

item1,"description 1"
item2,"description 2"
item3,"description 3
description 3 continues on new line"
item4,"description 4"

应该像这样解析 有

item1,"description 1"
item2,"description 2"
item3,"description 3 description 3 continues on new line"
item4,"description 4"

没有办法在 PHP 中解析具有多行值的 CSV?

Consider s CSV file like this

item1,"description 1"
item2,"description 2"
item3,"description 3
description 3 continues on new line"
item4,"description 4"

which should be parsed like this

item1,"description 1"
item2,"description 2"
item3,"description 3 description 3 continues on new line"
item4,"description 4"

Is there a way to parse this CSV in PHP which has multiline values?

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

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

发布评论

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

评论(4

指尖上得阳光 2024-10-20 08:45:11

以下是一些如何做到这一点的工作示例。我正在使用 fgetcsv:

CSV in 字符串变量 $CsvString:

$fp = tmpfile();
fwrite($fp, $CsvString);
rewind($fp); //rewind to process CSV
while (($row = fgetcsv($fp, 0)) !== FALSE) {
    print_r($row);
}

CSV in file:

if (($handle = fopen("test.csv", "r")) !== FALSE) {
  while (($row = fgetcsv($handle, 0, ",")) !== FALSE) {
    print_r($row);
  }
  fclose($handle);
}

Here are some working examples how to do it. I am using fgetcsv:

CSV in string variable $CsvString:

$fp = tmpfile();
fwrite($fp, $CsvString);
rewind($fp); //rewind to process CSV
while (($row = fgetcsv($fp, 0)) !== FALSE) {
    print_r($row);
}

CSV in file:

if (($handle = fopen("test.csv", "r")) !== FALSE) {
  while (($row = fgetcsv($handle, 0, ",")) !== FALSE) {
    print_r($row);
  }
  fclose($handle);
}
你的心境我的脸 2024-10-20 08:45:11

fgetcsv 应该能够正确解析它。
无论如何,我不建议手动执行此操作,解析 CSV 时有很多这样的问题。

fgetcsv should be able to properly parse this.
I wouldn't recommend doing this by hand anyway, there are many such gotchas in parsing CSV.

夏末染殇 2024-10-20 08:45:11

基于 StanleyD 答案,但使用临时内存块(而不是写入磁盘)以获得更好的性能:

$fp = fopen('php://temp','r+');
fwrite($fp, $CsvString);
rewind($fp); //rewind to process CSV
while (($row = fgetcsv($fp, 0)) !== FALSE) {
    print_r($row);
}

Based on StanleyD answer, but using a temp memory block (rather than writing to disk) for better performance:

$fp = fopen('php://temp','r+');
fwrite($fp, $CsvString);
rewind($fp); //rewind to process CSV
while (($row = fgetcsv($fp, 0)) !== FALSE) {
    print_r($row);
}
(り薆情海 2024-10-20 08:45:11

这是使用 PHP 函数 str_getcsv( )

下面是一个示例:

function parse_csv( $filename_or_text, $delimiter=',', $enclosure='"', $linebreak="\n" )
{
    $return = array();
    
    if(false !== ($csv = (filter_var($filename_or_text, FILTER_VALIDATE_URL) ? file_get_contents($filename_or_text) : $filename_or_text)))
    {
        $csv = trim($csv);
        $csv = mb_convert_encoding($csv, 'UTF-16LE');   
        
        foreach(str_getcsv($csv, $linebreak, $enclosure) as $row){
            $col = str_getcsv($row, $delimiter, $enclosure);
            $col = array_map('trim', $col);
            $return[] = $col;
        }
    }
    else
    {
        throw new \Exception('Can not open the file.');
        $return = false;
    }
    
    return $return;
}

想象一下这样一种情况,您需要一个可以同时处理 URL 和逗号分隔文本的函数。这正是这样工作的函数。只需插入 CSV URL 或逗号分隔的文本即可正常工作。

Here is a quick and easy solution using the PHP function str_getcsv()

Here is an example:

function parse_csv( $filename_or_text, $delimiter=',', $enclosure='"', $linebreak="\n" )
{
    $return = array();
    
    if(false !== ($csv = (filter_var($filename_or_text, FILTER_VALIDATE_URL) ? file_get_contents($filename_or_text) : $filename_or_text)))
    {
        $csv = trim($csv);
        $csv = mb_convert_encoding($csv, 'UTF-16LE');   
        
        foreach(str_getcsv($csv, $linebreak, $enclosure) as $row){
            $col = str_getcsv($row, $delimiter, $enclosure);
            $col = array_map('trim', $col);
            $return[] = $col;
        }
    }
    else
    {
        throw new \Exception('Can not open the file.');
        $return = false;
    }
    
    return $return;
}

Imagine a situation where you need a function that works with both URL and comma delimited text. This is exactly the function that works like that. Just simply insert a CSV URL or comma separated text and it work nicely.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文