2013-07-14 35 views
0

好吧,我有這個腳本應該從http://www.omdbapi.com/PHP腳本只能在文件的最後一行

<?php 
$doc = new DOMDocument; 
$filePath = 'data.txt'; 

    if(file_exists($filePath)) { 
     $file = fopen($filePath,'r'); 
     while(!feof($file)) { 
      $name = fgets($file); 
      echo $name; 
      $doc->loadhtmlfile('http://www.omdbapi.com/?i=&t=' . $name . '&r=JSON&plot=full'); 

      $body = $doc->getElementsByTagName('body'); 
      if ($body && 0<$body->length) { 
       $body = $body->item(0); 
       echo $doc->savehtml($body); 
       } 
       sleep(1); 
     } 
     fclose($file); 
    } 
    else { 
     echo "FNF!"; 
    } 
?> 

越來越結果這是劇本,這是我使用

Planet Earth 
Game of Thrones 
Breaking Bad 
The Wire 
Life 
Sherlock 
Arrested Development 
Clanul Soprano 
Firefly 
Dexter 
Avatar: The Last Airbender 
Twin Peaks 
Freaks and Geeks 
Monty Python's Flying Circus 
Roma 
The Twilight Zone 
Oz 
Seinfeld 
Deadwood 
Desu n&#xF4;to 
House of Cards 
South Park 
Fawlty Towers 
Batman 
Prietenii tai 
Leyla ile Mecnun 
Cowboy Bebop 
Suits 
It's Always Sunny in Philadelphia 
Blackadder Goes Forth 
Six Feet Under 
Downton Abbey 
The Office 
The Walking Dead 
Archer 
Dragon Ball Z 
Black-Adder II 
Familia Simpson 
Top Gear 
Boardwalk Empire 
The X Files 
Battlestar Galactica 
Justified 
Black Adder the Third 
Futurama 
Doctor Who 
Spartacus: Blood and Sand 
Mad Men 
Dragon Ball Z 
Spaced 
文件

而且除了最後一個標題外,我還爲每個標題找到了「電影未找到」(這與我放在文件中的最後一個標題無關)。 你能告訴我爲什麼會發生這種情況?

回答

2

嘗試:

$name = trim(fgets($file)); 

當你調用fgets它的返回線路,並在最後的換行符。最後一行沒有換行符。

+0

這樣做。謝謝! 但我想知道爲什麼,因爲當我使用'echo $ name'時,標題周圍沒有任何空格。 – user2419750

+0

這不是空格,它是換行符。看起來很難看,但它在那裏:) –

+0

下次我會記住這一點。謝謝! – user2419750

相關問題