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 に答える