国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 網站 > 建站經驗 > 正文

p-hp實現讀取和寫入tab分割的文件

2019-11-02 14:32:58
字體:
來源:轉載
供稿:網友

   本文實例講述了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
月球未解之謎[www.9-39.com/html/mystery/]
y_shift($line); $array[$key] = $line; } else { $array[] = $line; } } } return $array; } /* ** Example usage: */ $array = array( 'line1' => array('data-1-1', 'data-1-2', 'data-1-3'), 'line2' => array('data-2-1', 'data-2-2', 'data-2-3'), 'line3' => array('data-3-1', 'data-3-2', 'data-3-3'), 'line4' => 'foobar', 'line5' => 'hello world' ); // save the array to the data.txt file: write_tabbed_file('data.txt', $array, true); /* the data.txt content looks like this: line1 data-1-1 data-1-2 data-1-3 line2 data-2-1 data-2-2 data-2-3 line3 data-3-1 data-3-2 data-3-3 line4 foobar line5 hello world */ // load the saved array: $reloaded_array = load_tabbed_file('data.txt',true); print_r($reloaded_array); // returns the array from above
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 武威市| 浏阳市| 陈巴尔虎旗| 安宁市| 维西| 屏山县| 新安县| 龙海市| 长治市| 察哈| 栾川县| 灵璧县| 阿拉善右旗| 东城区| 曲沃县| 桦甸市| 深泽县| 织金县| 台北市| 沙湾县| 轮台县| 修水县| 浏阳市| 蕲春县| 阳原县| 永善县| 孟州市| 平江县| 洛浦县| 朝阳市| 丹巴县| 嘉黎县| 柏乡县| 库尔勒市| 山东| 汶上县| 安平县| 大田县| 兴安县| 惠来县| 吉首市|