正则表达式未关闭报价

发布于 2024-09-11 10:55:47 字数 309 浏览 17 评论 0 原文

我正在尝试找出一种在 PHP 中读取包含返回值的 CSV 的方法。问题是,当您像这样读取文件时:

if (($handle = fopen($file, "r")) !== FALSE) {
 while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        row data...
    }
}

如果您在 CSV 中进行了重新调整,则它不起作用,它只会将返回视为新行。

我的想法是使用正则表达式来检查未关闭的引号,但我不知道类似的事情。有什么想法吗?

I am trying to figure out a way to read a CSV with returns in it in PHP. The problem is when you read the file like this:

if (($handle = fopen($file, "r")) !== FALSE) {
 while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        row data...
    }
}

If you have a retun in the CSV it does not work, it just sees the returns as a new row.

My idea was to have a regex to check for unclosed quotes, but I dont know of anything like that. Any ideas?

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

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

发布评论

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

评论(1

离旧人 2024-09-18 10:55:47

由于内置 fgetcsv 似乎无法正确处理 CSV 标准,因此在 fgetcsv 的 PHP 手册页 - 这是其中之一:

来自 http://www.php.net/manual/en/function.fgetcsv.php

PHP 的 CSV 处理内容是
不符合标准并与之相矛盾
RFC4180,因此 fgetcsv() 不能
正确处理这样的文件
示例...

有一种快速而肮脏的方式
符合 RFC 标准的 CSV 实现
创建和解析:

<?php 
function array_to_csvstring($items, $CSV_SEPARATOR = ';', $CSV_ENCLOSURE = '"', $CSV_LINEBREAK = "\n") { 
  $string = ''; 
  $o = array(); 

  foreach ($items as $item) { 
    if (stripos($item, $CSV_ENCLOSURE) !== false) { 
      $item = str_replace($CSV_ENCLOSURE, $CSV_ENCLOSURE . $CSV_ENCLOSURE, $item); 
    } 

    if ((stripos($item, $CSV_SEPARATOR) !== false) 
     || (stripos($item, $CSV_ENCLOSURE) !== false) 
     || (stripos($item, $CSV_LINEBREAK !== false))) { 
      $item = $CSV_ENCLOSURE . $item . $CSV_ENCLOSURE; 
    } 

    $o[] = $item; 
  } 

  $string = implode($CSV_SEPARATOR, $o) . $CSV_LINEBREAK; 

  return $string; 
} 

function csvstring_to_array(&$string, $CSV_SEPARATOR = ';', $CSV_ENCLOSURE = '"', $CSV_LINEBREAK = "\n") { 
  $o = array(); 

  $cnt = strlen($string); 
  $esc = false; 
  $escesc = false; 
  $num = 0; 
  $i = 0; 
  while ($i < $cnt) { 
    $s = $string[$i]; 

    if ($s == $CSV_LINEBREAK) { 
      if ($esc) { 
        $o[$num] .= $s; 
      } else { 
        $i++; 
        break; 
      } 
    } elseif ($s == $CSV_SEPARATOR) { 
      if ($esc) { 
        $o[$num] .= $s; 
      } else { 
        $num++; 
        $esc = false; 
        $escesc = false; 
      } 
    } elseif ($s == $CSV_ENCLOSURE) { 
      if ($escesc) { 
        $o[$num] .= $CSV_ENCLOSURE; 
        $escesc = false; 
      } 

      if ($esc) { 
        $esc = false; 
        $escesc = true; 
      } else { 
        $esc = true; 
        $escesc = false; 
      } 
    } else { 
      if ($escesc) { 
        $o[$num] .= $CSV_ENCLOSURE; 
        $escesc = false; 
      } 

      $o[$num] .= $s; 
    } 

    $i++; 
  } 

//  $string = substr($string, $i); 

  return $o; 
} 
?> 

Since it seems the built-in fgetcsv does not correctly handle the CSV standard, there are suggestions for alternatives on the PHP man page for fgetcsv - here's one of them:

From http://www.php.net/manual/en/function.fgetcsv.php

The PHP's CSV handling stuff is
non-standard and contradicts with
RFC4180, thus fgetcsv() cannot
properly deal with files like this
example ...

There is a quick and dirty
RFC-compliant realization of CSV
creation and parsing:

<?php 
function array_to_csvstring($items, $CSV_SEPARATOR = ';', $CSV_ENCLOSURE = '"', $CSV_LINEBREAK = "\n") { 
  $string = ''; 
  $o = array(); 

  foreach ($items as $item) { 
    if (stripos($item, $CSV_ENCLOSURE) !== false) { 
      $item = str_replace($CSV_ENCLOSURE, $CSV_ENCLOSURE . $CSV_ENCLOSURE, $item); 
    } 

    if ((stripos($item, $CSV_SEPARATOR) !== false) 
     || (stripos($item, $CSV_ENCLOSURE) !== false) 
     || (stripos($item, $CSV_LINEBREAK !== false))) { 
      $item = $CSV_ENCLOSURE . $item . $CSV_ENCLOSURE; 
    } 

    $o[] = $item; 
  } 

  $string = implode($CSV_SEPARATOR, $o) . $CSV_LINEBREAK; 

  return $string; 
} 

function csvstring_to_array(&$string, $CSV_SEPARATOR = ';', $CSV_ENCLOSURE = '"', $CSV_LINEBREAK = "\n") { 
  $o = array(); 

  $cnt = strlen($string); 
  $esc = false; 
  $escesc = false; 
  $num = 0; 
  $i = 0; 
  while ($i < $cnt) { 
    $s = $string[$i]; 

    if ($s == $CSV_LINEBREAK) { 
      if ($esc) { 
        $o[$num] .= $s; 
      } else { 
        $i++; 
        break; 
      } 
    } elseif ($s == $CSV_SEPARATOR) { 
      if ($esc) { 
        $o[$num] .= $s; 
      } else { 
        $num++; 
        $esc = false; 
        $escesc = false; 
      } 
    } elseif ($s == $CSV_ENCLOSURE) { 
      if ($escesc) { 
        $o[$num] .= $CSV_ENCLOSURE; 
        $escesc = false; 
      } 

      if ($esc) { 
        $esc = false; 
        $escesc = true; 
      } else { 
        $esc = true; 
        $escesc = false; 
      } 
    } else { 
      if ($escesc) { 
        $o[$num] .= $CSV_ENCLOSURE; 
        $escesc = false; 
      } 

      $o[$num] .= $s; 
    } 

    $i++; 
  } 

//  $string = substr($string, $i); 

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