【问题标题】:I want to store tweets that I find after a search, in a text file我想将搜索后找到的推文存储在文本文件中
【发布时间】:2013-01-20 08:48:04
【问题描述】:

有谁知道在 .txt 文件中搜索后如何保存推文? 我的索引文件如下:

     <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
     <html xmlns="http://www.w3.org/1999/xhtml">
     <head>
     <title>Twitter</title>
     </head>
     <body>
  <?php
     require('twitter.class.php');
     $twitter = new twitter_class();
     echo $twitter->getTweets('tomato', 15);
   ?>
   </body>
   </html>

我对这一切都很陌生,所以我会很感激任何帮助。

【问题讨论】:

    标签: php html twitter store


    【解决方案1】:

    这是保存推文的代码:

    <?php
        require('twitter.class.php');
        $twitter = new twitter_class();
        $tweets = $twitter->getTweets('tomato', 15);
        $currentfile = file_get_contents('tweets.txt');
        file_put_contents('tweets.txt', $currentfile.$tweets);
    ?>
    

    如果您不想附加推文,这将附加推文而不是删除数据,只需执行以下操作:

    <?php
        require('twitter.class.php');
        $twitter = new twitter_class();
        $tweets = $twitter->getTweets('tomato', 15);
        file_put_contents('tweets.txt', $tweets);
    ?>
    

    【讨论】:

    • 非常感谢!现在我有一个包含 html 的 .txt 文件,您对如何在“用户友好”模式下再次显示它有任何想法吗?
    • 是的,我愿意 :),给我发电子邮件 c1d@mypin.im 我会回复一些可以帮助你的代码,因为我认为我不能在这里回答这个问题 :)跨度>
    【解决方案2】:

    fwrite() 是你的朋友。在你回显推文而不是回显它的循环中将它们写入文本文件

    【讨论】:

      【解决方案3】:

      你试过file_punt_contents这个功能吗?

      你可以这样做:

      <?php
          $file = 'tweets.txt';
      
          $tweets = $twitter->getTweets('tomato', 15);
      
          // Write the contents back to the file
          file_put_contents($file, $tweets);
      ?>
      

      More info.

      【讨论】:

      • 非常感谢!现在我有一个包含 html 的 .txt 文件,您对如何在“用户友好”模式下再次显示它有任何想法吗?
      【解决方案4】:

      您可以创建文件并在php中使用fwrite写入,这是一个简单的代码:

       $fp = fopen('data.txt', 'w');
          fwrite($fp, $twitter->getTweets('tomato', 15););
          fclose($fp);
      

      【讨论】:

      • 非常感谢! :) 我现在想专注于在浏览器上显示 .txt 文件。有什么想法吗?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-01
      • 2023-03-17
      • 1970-01-01
      • 2010-10-22
      相关资源
      最近更新 更多