2013-02-05 27 views
0

有誰知道如何在.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> 

我是新來的這一切,所以我希望得到任何幫助。

回答

0

這裏是代碼保存推文:

<?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); 
?> 

這將追加的tweet,而不是擦除數據的,如果你不想追加鳴叫只是這樣做:

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

非常感謝!現在我有一個.txt文件,它裏面的html,你有什麼想法如何在「用戶友好」模式下再次顯示它? –

+0

是的,我是:),給我發電子郵件[email protected],我會回覆一些可以幫助你的代碼,因爲我不認爲我可以在這裏回答這個問題:) – C1D

0

fwrite()是你的朋友。在循環中,您回覆鳴叫而不是回顯它將它們寫入文本文件

0

您是否嘗試過函數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.

+0

太謝謝你了!現在我有一個.txt文件,它裏面的html,你有什麼想法如何在「用戶友好」模式下再次顯示它? –

0

您可以創建文件,並寫入在PHP usine fwrite,這裏是一個簡單的代碼:

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

非常感謝! :)我現在想專注於在瀏覽器上顯示.txt文件。有任何想法嗎 ? –