我在 Windows 系统上有一个 csv 文件,我是从 Mac 获取的,数据看起来像
ID526526526526
Run Code Online (Sandbox Code Playgroud)
所以你可以看到没有新行没有分隔符,但它仍然说 csv 文件。但是我有以下代码
$csvFile = 'MonC1.csv';
$allRowsAsArray = array();
if (!$fp=fopen($csvFile,"r")) echo "The file could not be opened.<br/>";
while (( $data = fgetcsv ( $fp , 0 , ',')) !== FALSE )
{
$allRowsAsArray[] = $data;
}
echo '<pre>';
print_r($allRowsAsArray );
echo '</pre>';
Run Code Online (Sandbox Code Playgroud)
现在,当我在浏览器中打印数据时,它看起来像
Array
(
[0] => Array
(
[0] => ID
526
526
526
)
);
Run Code Online (Sandbox Code Playgroud)
现在我被卡住了,因为我无法使用 fputcsv 创建另一个带有新行的正确分隔的 csv 文件。
任何帮助都受到高度欢迎。提前致谢。
检测行尾时出现问题。auto_detect_line_endings运行前尝试开启fopen。
ini_set('auto_detect_line_endings', TRUE);
$csvFile = 'MonC1.csv';
$allRowsAsArray = array();
if (!$fp=fopen($csvFile,"r")) echo "The file could not be opened.<br/>";
while (( $data = fgetcsv ( $fp , 0 , ',')) !== FALSE )
{
$allRowsAsArray[] = $data;
}
echo '<pre>';
print_r($allRowsAsArray );
echo '</pre>';
Run Code Online (Sandbox Code Playgroud)