2012-07-28 62 views
0

你好,如何阅读使用PHP和TXT文件替换数组? 我想读的文件是这样的:如何阅读文本文件和翻译fild

||Search||,||s||---||Images||,||i|| 

这是我的PHP代码:

$f = fopen("test.txt", "r"); 
$image= "Imgaes"; 
// Read line by line until end of file 
while (!feof($f)) { 
    // Make an array using comma as delimiter 
    // $arrM = explode("---",fgets($f)); 
    $arr = explode("||---||",fgets($f)); 
    // Write links (get the data in the array) 
    $num = 1; 
    while($num <= 30) { 
     list($eng,$fa) = explode("||,||", $arr[$num]); 
     foreach($html->find('html') as $full) { 
      $all = $full->innertext; 
     } 
     $fe = str_replace($eng,$fa,$all); 
     $num = $num+1; 
    } 
    echo $fe; 
}  
fclose($f); 

但这不工作:(!!!

+0

“this does not work”is not a valid explanation for your problem。给我们提供错误信息,结果你g et,以及预期的结果。 – Jocelyn 2012-07-28 15:14:28

+0

不知道问题,但这看起来不正确:$ image =“Imgaes”; – 2012-07-28 15:20:45

+0

没有这个$ image只是测试我想用简单的html圆顶解析页面,并用这个文本文件翻译所有单词 – 2012-07-28 15:23:17

回答

1

的爆炸,解析您的这个功能数据之前:

// Function arrayData 
function arrayData($data){ 
     # code... 
     // Create an array out of each line 
     $data_array=explode("\n", $data); 
     // Find the last key in the array 
     $last_key=count($data_array)-1; 
     // If the last line is empty revise the last key downwards until there's actually something there 
     while(empty($data_array[$last_key])) 
     { 
      $last_key-=1; 
     } 
     // Figure out the first key based upon the value set for the number of lines to display 
     $first_key=$last_key-($this->num_lines-1); 
     // Start a new array to store the last X lines in 
     $final_array=array(); 

     // Work through the array and only add the last X lines to it. 
     foreach($data_array as $key => $value) 
     { 
      if($key >= $first_key && $key <= $last_key) 
      { 
      $final_array[]=$value; 
      } 
     } 
    return $final_array; 
    } // end function