本文实例讲述了php实现读取和写入tab分割的文件。分享给大家供大家参考。具体分析如下:
这段php代码实现读取和写入tab分割的文件,包含两个独立的函数,一个读,一个写,例如cvs文件等
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 // // save an array as tab seperated text file // function write_tabbed_file($filepath, $array, $save_keys=false){ $content = ''; reset($array); while(list($key, $val) = each($array)){ // replace tabs in keys and values to [space] $key = str_replace("t", " ", $key); $val = str_replace("t", " ", $val); if ($save_keys){ $content .= $key."t"; } // create line: $content .= (is_array($val)) ? implode("t", $val) : $val; $content .= "n"; } if (file_exists($filepath) && !is_writeable($filepath)){ return false; } if ($fp = fopen($filepath, 'w+')){ fwrite($fp, $content); fclose($fp); } else { return false; } return true; } // // load a tab seperated text file as array // function load_tabbed_file($filepath, $load_keys=false){ $array = array(); if (!file_exists($filepath)){ return $array; } $content = file($filepath); for ($x=0; $x < count($content); $x++){ if (trim($content[$x]) != ''){ $line = explode("t", trim($content[$x])); if ($load_keys){ $key = arra新闻热点
疑难解答