php - 在 PHP 中解析 CSV 并尝试在字段内容中保留换行符

标签 php csv

我有一个 csv 文件,其中一列有换行符。该列是一个描述字段,因此它存储文本行、项目符号点和最重要的换行符——有时在段落之间有两个。使事情复杂化的是,描述字段还包含引号。

我已尽我所能将内容(并保持格式)放入变量中。我试过 file_get_contents、str_getcsv 和 fgetcsv 都无济于事。其中一个函数让我将整个描述字段放入一个变量中,但它删除了所有新行,所以所有内容都在一个大段落中。另一个单独解析描述字段中的每个段落,而不是一个单独的单元。

这是我的 csv 文件的一般格式:

"391","The Great Gatsby","The Great Gatsby, F. Scott Fitzgerald’s third book, stands as the supreme achievement of his career. 

This exemplary novel of the Jazz Age has been acclaimed by generations of readers. The story of the fabulously wealthy Jay Gatsby and his love for the beautiful Daisy Buchanan, of lavish parties on Long Island at a time when The New York Times noted “gin was the national drink and sex the national obsession,” it is an exquisitely crafted tale of America in the 1920s.

The Great Gatsby is one of the great classics of twentieth-century literature.","No","Yes",

我想要一个变量,其中 $array[0]=391$array[1]=The Great Gatsby 最重要的是 $ array[2] 包含描述中的文本以及所有换行符和格式,包括字段本身内的引号。

或者如果有更好的方法使用 PHP 解析它,请告诉我。

最佳答案

如果有人在 2019 年仍然遇到这个问题,我的解决方案是 fgetcsv($file)。自动遵守文本字段内的换行符。

关于php - 在 PHP 中解析 CSV 并尝试在字段内容中保留换行符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25219967/

相关文章:

vba - 从 VBA 中的数组创建 CSV

python - 如何使用sql读取csv

php - 尝试使用php访问远程数据库

python - 在 python 3 上添加两个日期

python - 求和一列值,包括 Python 中的字母

带有字符串分隔符(多字符)的 Java CSV 解析器

php - Gitignore Laravel 供应商文件夹

php - 在Mysql表创建中包含外键或在php应用程序中包含链接表?

php - Xpath选择多个标签

php - 有没有我可以用来表示实例化后添加的魔术方法属性的 PHP DocBlock?