我有一个 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)
。自动遵守文本字段内的换行符。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)