如何阅读文本文件并翻译fild

时间:2012-07-28 14:42:59

标签: php html replace translate

您好,如何使用php读取txt文件并用数组替换? 我想读这样的文件:

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

但这不起作用:(!!!

1 个答案:

答案 0 :(得分: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