首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PHP脚本仅适用于文件的最后一行

PHP脚本仅适用于文件的最后一行
EN

Stack Overflow用户
提问于 2013-07-14 14:48:00
回答 1查看 67关注 0票数 0

好的,我有这个脚本,它应该可以从http://www.omdbapi.com/获取结果

代码语言:javascript
复制
<?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!";
    }
?>

这是脚本,这是我正在使用的文件

代码语言:javascript
复制
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

除了最后一个标题之外,我的每个标题都会显示“找不到电影”(我把哪个标题放在文件的最后一个并不重要)。你能告诉我为什么会这样吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-07-14 14:56:29

尝试:

代码语言:javascript
复制
$name = trim(fgets($file));

当你调用fgets时,它会返回换行符在末尾的那一行。最后一行没有换行符。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17637207

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档