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

首頁 > 開發 > PHP > 正文

php實現讀取和寫入tab分割的文件

2024-05-04 23:35:48
字體:
來源:轉載
供稿:網友

這篇文章主要介紹了php實現讀取和寫入tab分割的文件,涉及php文件讀寫及字符串操作的相關技巧,需要的朋友可以參考下

本文實例講述了php實現讀取和寫入tab分割的文件。分享給大家供大家參考。具體分析如下:

這段php代碼實現讀取和寫入tab分割的文件,包含兩個獨立的函數,一個讀,一個寫,例如cvs文件等

 

 
  1. // 
  2. // save an array as tab seperated text file 
  3. // 
  4. function write_tabbed_file($filepath$array$save_keys=false){ 
  5. $content = ''
  6. reset($array); 
  7. while(list($key$val) = each($array)){ 
  8. // replace tabs in keys and values to [space] 
  9. $key = str_replace("/t"" "$key); 
  10. $val = str_replace("/t"" "$val); 
  11. if ($save_keys){ $content .= $key."/t"; } 
  12. // create line: 
  13. $content .= (is_array($val)) ? implode("/t"$val) : $val
  14. $content .= "/n"
  15. if (file_exists($filepath) && !is_writeable($filepath)){  
  16. return false; 
  17. if ($fp = fopen($filepath'w+')){ 
  18. fwrite($fp$content); 
  19. fclose($fp); 
  20. else { return false; } 
  21. return true; 
  22. // 
  23. // load a tab seperated text file as array 
  24. // 
  25. function load_tabbed_file($filepath$load_keys=false){ 
  26. $array = array(); 
  27. if (!file_exists($filepath)){ return $array; } 
  28. $content = file($filepath); 
  29. for ($x=0; $x < count($content); $x++){ 
  30. if (trim($content[$x]) != ''){ 
  31. $line = explode("/t", trim($content[$x])); 
  32. if ($load_keys){ 
  33. $key = array_shift($line); 
  34. $array[$key] = $line
  35. else { $array[] = $line; } 
  36. return $array
  37. /* 
  38. ** Example usage: 
  39. */ 
  40. $array = array
  41. 'line1' => array('data-1-1''data-1-2''data-1-3'), 
  42. 'line2' => array('data-2-1''data-2-2''data-2-3'), 
  43. 'line3' => array('data-3-1''data-3-2''data-3-3'), 
  44. 'line4' => 'foobar'
  45. 'line5' => 'hello world' 
  46. ); 
  47. // save the array to the data.txt file: 
  48. write_tabbed_file('data.txt'$array, true); 
  49. /* the data.txt content looks like this: 
  50. line1 data-1-1 data-1-2 data-1-3 
  51. line2 data-2-1 data-2-2 data-2-3 
  52. line3 data-3-1 data-3-2 data-3-3 
  53. line4 foobar 
  54. line5 hello world 
  55. */ 
  56. // load the saved array: 
  57. $reloaded_array = load_tabbed_file('data.txt',true); 
  58. print_r($reloaded_array); 
  59. // returns the array from above 

希望本文所述對大家的php程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 新河县| 乐清市| 成安县| 杭锦后旗| 平和县| 伽师县| 信丰县| 麻城市| 抚顺县| 博湖县| 扎囊县| 长顺县| 贵德县| 丰原市| 威海市| 南川市| 炎陵县| 新田县| 固阳县| 沙洋县| 延安市| 永新县| 汶川县| 天峨县| 翁牛特旗| 灌云县| 尉氏县| 建德市| 铜陵市| 轮台县| 滨州市| 钦州市| 绥芬河市| 年辖:市辖区| 岱山县| 于都县| 昭通市| 宣威市| 哈密市| 泉州市| 彰武县|