我有一个 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/