I have a text file like....
I want to convert it to CSV with the help of a little bit PHP, and I want to know also how it can be reversed...ie from CSV to an ordered, or un-ordered list.....kindly help me please :)
I have a text file like....
I want to convert it to CSV with the help of a little bit PHP, and I want to know also how it can be reversed...ie from CSV to an ordered, or un-ordered list.....kindly help me please :)
<?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>';
Edit: I made some modification to work with double newlines.
To convert to CSV
$data=file_get_contents("file");
$data =explode("\n\n",$data);
echo implode(",",array_filter($data));
Update as required to convert from CSV,
$data = explode(",", file_get_contents("file") );
echo implode("\n\n",$data);
For many rows of csv data, you can iterate the file using fgetcsv(). eg
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);
}