0

您好,如何使用 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);

但这不起作用:(!!!

4

1 回答 1

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
于 2012-07-28T15:57:16.230 回答