2010-09-30 56 views
1

我有這樣的代碼:PHP負載陣列

require("class.XMLHttpRequest.php"); 
function hot($news){ 
$url="https://localhost/search.aspx?search=".$news.""; 
$ajax=new XMLHttpRequest(); 
$ajax->setRequestHeader("Cookie","Cookie: host"); 
$ajax->open("GET",$url,true); 
$ajax->send(null); 
if($ajax->status==200){ 
    $rHeader=$ajax->getResponseHeader("Set-Cookie"); 
    if(substr_count($rHeader, "Present!")>0) { return true; } 
}else{ return false; } 
} 

$celebrities = array('britney','gaga','carol'); 
$filename = 'result.txt'; 
$handle = fopen($filename, 'a'); 
foreach($celebrities as $celebrity) 
{ 
    if(hot($celebrity)) { fwrite($handle, "{$celebrity}\r\n"); }; 
} 
fclose($handle); 

代替

$celebrities = array('britney','gaga','carol'); 
$filename = 'result.txt'; 
$handle = fopen($filename, 'a'); 
foreach($celebrities as $celebrity) 
{ 
    if(hot($celebrity)) { fwrite($handle, "{$celebrity}\r\n"); }; 
} 
fclose($handle); 

我想要實現

$read_file = fopen('array.txt', 'r'); 
$write_file = fopen('result.txt', 'a'); 

while(!feof($read_file)) 
{ 
    $celebrity = fgets($read_file); 
    if(hot($celebrity)) { fwrite($handle, "{$celebrity}\r\n"); } 
} 

fclose($write_file); 
fclose($read_file); 

現在的Result.txt犯規被寫入和現在,我哪裏出錯了? 腳本應該從一個文件中讀取一個數組,然後處理該函數,然後將每個重新寫入的結果寫在result.txt中的新行中 在此先感謝!

回答

1

假設你每行有一個名人:

$celebrity = trim(fgets($read_file)); 

由於fgets將返回一個字符串包括換行符。

+0

感謝,它的工作 – adam 2010-09-30 15:59:36

0

就行了:

if(hot($celebrity)) { fwrite($handle, "{$celebrity}\r\n"); } 

您使用$handle,它不再定義。你想使用$write_file代替:

if(hot($celebrity)) { fwrite($write_file, "{$celebrity}\r\n"); } 
+0

感謝,我forogot一下吧 – adam 2010-09-30 16:00:47