2012-08-06 35 views
0

玉傢伙又需要你的幫助,解析陣列到收藏夾評論

以前大家給我介紹了其收藏一些調整後,已經很大。除了使用我的PHP代碼,似乎沒有一種方法來添加標題的圖像。現在我的一個朋友向我介紹使用.txt文件進行數組。現在這一切都很好,但我似乎無法得到我們想要正確讀取文件的代碼。目前它是隨機抽取字母「a」和字母「p」並指定的,我不知道它在哪裏得到這個。

現在這裏是我想出來的文件的內容的代碼。

<?php 
    // process caption file into named array 

    //open the file 
    $myFile = "captions.txt"; 
    $fh = fopen($myFile, 'r') or die("Can't open file"); 

    $theData = explode(fread($fh, filesize($myFile)),"\n"); 


    //close the file 
    fclose($fh); 


    //parse line by line until there is no data left. 
    foreach ($theData as $item => $line) { 
    $exploded = explode("=", $line); 
    if (count($exploded) == 2) { 
    $myFile[$exploded[0]] = $exploded[1]; 
    } 
    } 



    ?> 

然後我使用的代碼自動填充我的圖片相冊反過來激活lighbtox。

<?php 

        $images = glob('*.{jpg,jpeg,png,gif}', GLOB_BRACE); 

        foreach ($images as $image) { 
         if (file_exists("./thumbs/{$image}")){ 
          echo "<a href=\"{$image}\" rel=\"lightbox[gallery]\" title=\"" . $myFile[$image] . "\" style=\"margin-left:25px; margin-right:25px; margin-top:30px; display:inline-block; border:5px solid #fff;\"><img src=\"thumbs/{$image}\" alt=\"{$image}\" /></a>"; 
         } 
        } 


        ?> 

使用此代碼生成任何錯誤,但不正確讀取的字幕文件。

我想要做的就是文本文件的安裝與文件名分開a =和然後標題。

這裏是我的測試頁面的鏈接,如果有人想看一看。

http://outtamymindphoto.myftp.org/images/testalbum/testpage.php

回答

1

你應該通過固定這一行開始:

$theData = explode(fread($fh, filesize($myFile)),"\n"); 

按照PHP手冊中,分隔符是第一個參數。

(array explode (string $delimiter , string $string [, int $limit ])) 

(瞭解更多關於explode - http://php.net/manual/en/function.explode.php

正確的做法:

$theData = explode("\n" , fread($fh, filesize($myFile))); 

你也應該儘量輸出,以找出問題的變量。 例如,使用var_dump($var)來檢查$var的值。

希望我幫你, 評論,如果你需要進一步的幫助。

+0

你好,我的朋友又看了看代碼,發現他犯了一些錯誤。他爲我重寫了它,並且記錄了他在做什麼,所以我可以從中學習,並與原文進行比較。新代碼包含了您建議的所有內容。謝謝 – Steven 2012-08-06 22:00:39

+0

不客氣。 – 2012-08-06 22:01:50