我有一个像......这样的文本文件。
arfc@net.xz
editor@magazine.com
nafi@cyber.net
nau@times.com
jai@mail.com
我希望借助一点点PHP将其转换为CSV,我想知道它是如何反转的...即从CSV到有序或无序列表......请帮助我:)
答案 0 :(得分:2)
转换为CSV
$data=file_get_contents("file");
$data =explode("\n\n",$data);
echo implode(",",array_filter($data));
根据需要更新以转换CSV,
$data = explode(",", file_get_contents("file") );
echo implode("\n\n",$data);
对于许多行的csv数据,您可以使用fgetcsv()迭代文件。例如
if (($handle = fopen("file.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 2048, ",")) !== FALSE) {
$num = count($data);
for ($c=0; $c < $num; $c++) {
echo $data[$c] . "<br />\n";
}
}
fclose($handle);
}
答案 1 :(得分:1)
<?php
//read file
$content = file_get_content($filteredFilePath);
//explode contents into array
//if you use windows or mac, newlines may be different
//i.e: \r, \r\n
$list = explode("\n\n", $content);
//iterate over the items and print a HTML list
//to generate ordered list, use: <ol>
echo '<ul>';
foreach($list as $item) {
echo '<li>' . $item . '</li>';
}
echo '</ul>';
编辑:我做了一些修改以使用双换行符。