如何从我的代码中删除 %0D 字符。

huangapple go评论73阅读模式
英文:

PHP. How to remove a %0D character from my code

问题

我正在尝试通过每日摘要的方式向自己发送一封包含一些RSS源的电子邮件。我收到了以下错误消息,由于多余的%0D字符导致的。您能建议我在代码中需要进行哪些更改以删除它吗?

非常感谢

错误信息为:simplexml_load_file(https://hdblog.it/feed%0D):无法打开流:无效的重定向URL!

英文:

I am trying to send me an email with a daily digest of some RSS feeds. I receive the following error, caused by an extra %0D character. Could you suggest me what I need to correct in the code to remove it?

Thanks a lot

The error is: simplexml_load_file(https://hdblog.it/feed%0D): failed to open stream: Invalid redirect URL!

// Set the timezone to Rome, Italy
date_default_timezone_set('Europe/Rome');

// Set the cutoff time to 24 hours ago
$cutoff_time = time() - (24 * 60 * 60);

// Set the subject of the email
$subject = "Digest RSS " . date("d/m/Y");

// Initialize the message
$message = "<html><body>";
$message .= "<h1>RSS Digest</h1>";
$message .= "<p>Ecco gli ultimi aggiornamenti dai tuoi feed RSS:</p>";

// Read the list of RSS feeds from a file
$rss_feeds_file = file_get_contents('rss_feeds.txt');
$rss_feeds = explode("\n", $rss_feeds_file);

// Loop through the list of RSS feeds
foreach ($rss_feeds as $rss_feed) {
  // Load the RSS feed
  $rss = simplexml_load_file($rss_feed);
  if ($rss) {
    // Initialize a flag to keep track of whether there are feed items to include in the email
    $items_included = false;

    // Add the feed title to the message
    $message .= "<h2>" . $rss->channel->title . "</h2>";
    // Add a list of items to the message
    $message .= "<ul>";

    // Loop through the feed items
    foreach($rss->channel->item as $item) {
      // Check if the item was published within the last 24 hours
      $item_time = strtotime($item->pubDate);
      if ($item_time > $cutoff_time) {
        // If it was, add the feed item to the email
        $message .= "<li><a href='" . $item->link . "'>" . $item->title . "</a><br />";
        $message .= strip_tags($item->description) . "</li>";
        $items_included = true;
      }
    }
    $message .= "</ul>";
  }
}

$message .= "</body></html>";

I am trying to send me an email with a daily digest of some RSS feeds. I receive the following error, caused by an extra %0D character. Could you suggest me what I need to correct in the code to remove it?

Thanks a lot

The error is: simplexml_load_file(https://hdblog.it/feed%0D): failed to open stream: Invalid redirect URL!

答案1

得分: 1

你现在是否在运行Windows?

%0D是回车符。您正在使用\n(换行符)拆分输入文件,但也许您的输入文件具有\r\n行终止符。尝试改为使用'\r\n'进行拆分。

如果您的输入文件是在与您运行代码的平台上创建的,您可以尝试使用php常量PHP_EOL

英文:

Are you running on Windows?

%0D is the carriage return character. You are exploding the input file on \n (line feed), but perhaps your input file has \r\n line terminators. Try exploding on '\r\n' instead.

If your input file has been created on the same platform as you are running your code you could try using the php constant PHP_EOL.

huangapple
  • 本文由 发表于 2023年1月9日 17:08:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/75055090.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定