2016-03-18 21 views
-1

我確實有一個名稱,年齡,類型,地址,電話&郵件和大約50行數據的五列的電話列表。數據採用csv格式(文件:my_data.csv)。從csv的php separte行

NAME AGE TYPE ADDRESS       PHONE Email 
Robert 36 Freind CA Avenue, jercy street, Machigon 3456231 [email protected] 
Calux 24 Lover new opera, bank steet,Machigaon  3456760 [email protected] 
Gandy 25 Lover Ninth house, dark square, machigaon 3458923 [email protected] 

對於我的項目,我將csv與php連接起來,我的php文件是:get-add.php。 下面的源代碼中給出:

<?php 
$check="Lover"; // to be checked in the third column 
    $handle = fopen("my_data.csv","r")or die("file dont exist"); 
    $output = ''; 
    while (!feof($handle)){ 
    $data = fgetcsv($handle,2096,","); 
     if(($data[2] ==$check)){ 
      $output .= sprintf("%s; Age: %d; %s; %s; Ph:%d; Mail: %s)<br>", 
      $data[0], $data[1], $data[2], $data[3], $data[4], $data[5]); 
     } 
    } 
echo $output; 
fclose($handle); 
?> 

,結果進來如下因素格式。

Calux; Age:24; Type:Lover new opera, bank steet,Machigaon; Ph:3456760; Mail:[email protected] 
Gandy Age:25; Type:Lover; Ninth house, dark square, machigaon; Ph:3458923; Mail:[email protected] 

雖然我需要以下格式。

Calux Age:24; Lover 
     new opera, bank steet,Machigaon 
     Ph:3456760 
     Mail: [email protected] 

Gandy Age:25; Lover 
     Ninth house, dark square, machigaon 
     Ph:3458923 
     Mail: [email protected] 

請幫助朋友們糾正php代碼。

回答

0

這取決於您輸出到的位置。如果是終端屏幕或文本文件,請在需要的地方添加\n,如果它是瀏覽器,則爲<br />。當然,如果你要輸出到瀏覽器,你可能想用HTML來格式化它。

而對於選項卡,它是\t,但不適用於HTML。這可以回到格式化。