2010-02-27 66 views
0

我有一個文本文件中像....如何更改列表CSV,反之亦然,在PHP

[email protected]

[email protected]

[email protected]

[email protected]

[email protected]

我想在一點PHP的幫助下將其轉換爲CSV,並且我也想知道它是如何顛倒的......即從CSV到有序或無序列表.....好心幫我請:)

+0

有序列表在PHP中?你是指數組還是HTML列表? – erenon 2010-02-27 12:34:14

+0

HTML格式的列表... – Jumani 2010-02-27 12:39:33

回答

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); 
} 
+0

嘿...你是男人!....這確實把列表轉換爲CSV ......謝謝! 這將是非常好的...如果你可以告訴相反的話.. :) – Jumani 2010-02-27 13:00:27

+0

$ data = file_get_contents(「csv」); $ data = explode(「,」,$ data); echo implode(「\ n \ n」,$ data);是精確的倒數:P – svens 2010-02-27 13:09:55

+0

現在一切都很好......除了一個......從CSV輸出到列表的結果如下: 「[email protected] [email protected] [email protected] 1 @名爲.abc xyz.com [email protected] [email protected] 「 ....當它shud像 」 [email protected] [email protected] [email protected]「 我知道這是一個容易解決的問題...只是無法通過它...承認是PHP中的n00b ...:/ – Jumani 2010-02-27 13:53:35

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>'; 

編輯:我做了一些修改,採用雙換行工作。

+0

我有一個名爲data.txt的文件和這個PHP腳本文件...在相同的目錄....並使用$ fileContents = file(「data.txt」);代替你的第三行...因爲我猜我必須提供外部文件路徑.... :) ....我認爲這將工作...但由於我的絕對開始階段,我cudnt能夠找到爲什麼它不工作....它顯示的只是一個子彈......或者在有序列表的情況下,只是「1」.....:S – Jumani 2010-02-27 12:46:50

+0

什麼樣的操作系統你用? – erenon 2010-02-27 12:51:10

+0

Windows 7,32位。並使用谷歌Chrome ... – Jumani 2010-02-27 12:53:06

相關問題