doubu4406 2014-10-15 06:29
浏览 35
已采纳

loadHTMLFile无法返回URL

<?php

    $url="http://somedomain/something";
    $lines = file('text.txt');
    foreach ($lines as $line_num => $line) {
    echo "Line #<b>{$line_num}</b> : " . htmlspecialchars($line) . "<br />
";
    $dom = new DOMDocument;
    $dom->loadHTMLFile($url.$line); //cannot load this
    foreach ($dom->getElementsByTagName('p') as $node) {
    // do stuff with $node
    echo $node->nodeValue, "
";    



    }
    }   


fclose($handle);

   ?>

I would like to attach the url to the lines of the text file:

Example

$url.$line1 (http://somedomain/something/line1)

$url.$line2 (http://somedomain/something/line2)

I have succesfully set up everything and the loop works well too. But whenever I try to concatenate the url with the line an HTTP 500 Error is returned

Also these characters '%0D%0A' attch themselves to end of the $url.$line and hence the error?

Any help appreciated

Update: The error is because of the '%0D%0A' characters somehow attaching themselves to the end of each url. Any idea how to remove them?

  • 写回答

1条回答 默认 最新

  • duangua5742 2014-10-15 17:44
    关注

    The extra characters you're getting, %0D%0A, are the carriage return and line feed characters for Windows computers. To get rid of them, you can use file with the FILE_IGNORE_NEW_LINES flag:

    $lines = file('text.txt', FILE_IGNORE_NEW_LINES);
    

    This will stop the carriage return characters being including on the end of each line in the array.

    Alternatively, you can chomp the line ending with rtrim:

    $line = rtrim($line);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题