2011-03-14 77 views
29

我有一個具有以下細節.txt文件:PHP - 解析一個txt文件

ID^NAME^DESCRIPTION^IMAGES 
123^test^Some text goes here^image_1.jpg,image_2.jpg 
133^hello^some other test^image_3456.jpg,image_89.jpg 

我想怎麼辦,是解析這個廣告得到的數值爲更可讀的格式,可能如果可能的話進入數組。

感謝

回答

55

你可以做到這一點很容易首先,你打開使用功能file_get_contents()的文本文件,然後你砍上使用功能explode()換行字符的字符串,這樣

$txt_file = file_get_contents('path/to/file.txt'); 
$rows  = explode("\n", $txt_file); 
array_shift($rows); 

foreach($rows as $row => $data) 
{ 
    //get row data 
    $row_data = explode('^', $data); 

    $info[$row]['id']   = $row_data[0]; 
    $info[$row]['name']   = $row_data[1]; 
    $info[$row]['description'] = $row_data[2]; 
    $info[$row]['images']  = $row_data[3]; 

    //display data 
    echo 'Row ' . $row . ' ID: ' . $info[$row]['id'] . '<br />'; 
    echo 'Row ' . $row . ' NAME: ' . $info[$row]['name'] . '<br />'; 
    echo 'Row ' . $row . ' DESCRIPTION: ' . $info[$row]['description'] . '<br />'; 
    echo 'Row ' . $row . ' IMAGES:<br />'; 

    //display images 
    $row_images = explode(',', $info[$row]['images']); 

    foreach($row_images as $row_image) 
    { 
     echo ' - ' . $row_image . '<br />'; 
    } 

    echo '<br />'; 
}

。這樣你將獲得一個所有行分開的數組。然後用功能array_shift()您可以刪除第一行,因爲它是標題。

獲取行後,您可以遍歷數組並將所有信息放入一個名爲$info的新數組中。然後,您將能夠從第零行開始獲取每行信息。因此,例如$info[0]['description']將是Some text goes here

如果你想把圖像放在一個數組中,你也可以使用explode()。如果你想要的東西更好

$values = explode('^', $string); 

或者:

$data = array(); 
$firstLine = true; 
foreach(explode("\n", $string) as $line) { 
    if($firstLine) { $firstLine = false; continue; } // skip first line 
    $row = explode('^', $line); 
    $data[] = array(
     'id' => (int)$row[0], 
     'name' => $row[1], 
     'description' => $row[2], 
     'images' => explode(',', $row[3]) 
    ); 
} 
+0

@Michiel佩特感謝平視,問題是,當我得到的圖像的var_dump()只輸出:IMAGES 123IMAGES 123IMAGES 123IMAGES 123IMAGES 123IMAGES 123IMAGES 123什麼想法? – terrid25 2011-03-14 14:27:21

+0

@ terrid25:你嘗試了我的新(更新)代碼嗎?如果是,請發佈你正在使用'var_dump()'的代碼。 – 2011-03-14 14:29:58

+0

@ terrid25:我使用下面的代碼:'var_export(explode(',',$ info [1] ['images']));'。它輸出:'array(0 =>'image_1.jpg',1 =>'image_2.jpg',)'。 – 2011-03-14 14:32:50

8

使用explode()fgetcsv()編輯版本,所以這裏是一個重做。這是最有可能使用脫字符號作爲分隔CSV文件,所以...

$fh = fopen('yourfile.txt'); 
$headers = fgetcsv($fh, 0, '^'); 
$details = array(); 
while($line = fgetcsv($fh, 0, '^')) { 
    $details[] = $line; 
} 
fclose($fh); 
+0

我如何訪問它們的值? – terrid25 2011-03-14 14:09:33

+0

學習PHP。 'foreach($ data as $ row)'然後例如'$ row ['id']' – ThiefMaster 2011-03-14 14:32:35

0
<?php 
$row = 1; 
if (($handle = fopen("test.txt", "r")) !== FALSE) { 
    while (($data = fgetcsv($handle, 1000, "^")) !== FALSE) { 
     $num = count($data); 
     echo "<p> $num fields in line $row: <br /></p>\n"; 
     $row++; 
     for ($c=0; $c < $num; $c++) { 
      echo $data[$c] . "<br />\n"; 
     } 
    } 
    fclose($handle); 
} 
?> 
+0

我該如何分割圖像? – terrid25 2011-03-14 14:04:45

0

好了,沒看到只要使用這個第一行:$first_row_images = explode(',', $info[0]['images']);

0

憂色列表,在爆炸字符串後拆分「image_1.jpg,image_2.jpg」:

list($number, $status, $text, $images) = explode("^", $data); 

$splited_images= preg_split(',', $images); 
4

到目前爲止,我最關注的最簡單的例子就是file()方法。

$array = file("myfile"); 
foreach($array as $line) 
     { 
      echo $line; 
     } 

這將顯示文件中的所有行,這也適用於遠程URL。

簡單明瞭。

REF:IBM PHP Parse

3

我想貢獻,提供原子數據結構的文件。

$lines = file('some.txt'); 
$keys = explode('^', array_shift($lines)); 
$results = array_map(
    function($x) use ($keys){ 
     return array_combine($keys, explode('^', trim($x))); 
    }, 
    $lines 
); 
+1

美麗!這也比其他方法更完整,因爲它實際上使用了標題行。 – demonkoryu 2014-01-07 17:20:54

0

我的解決方案

function parseTextFile($file){ 
    if(!$file = file_get_contents($file)) 
     throw new Exception('No file was found!!'); 
    $data = []; 
    $firstLine = true; 
    foreach(explode("\n", $file) as $line) { 
     if($firstLine) { 
      $keys=explode('^', $line); 
      $firstLine = false; 
      continue; 
     } // skip first line 
     $texts = explode('^', $line); 
     $data[] = array_combine($keys,$texts); 
    } 
    return $data; 
}