• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

php实现读写tab分割的文件

php 搞代码 4年前 (2022-01-22) 10次浏览 已收录 0个评论

本文主要介绍了php实现读取和写入tab分割的文件,涉及php文件读写及字符串操作的相关技巧。希望对大家有所帮助。

本文实例讲述了php实现读取和写入tab分割的文件。分享给大家供大家参考。具体分析如下:

这段php代码实现读取和写入tab分割的文件,包含两个独立的函数,一个读,一个写,例如cvs文件等

//// 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_exis<em>8本文来源gao.dai.ma.com搞@代*码(网$</em><pre>搞代gaodaima码

ts($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 = array_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-3line2 data-2-1 data-2-2 data-2-3line3 data-3-1 data-3-2 data-3-3line4 foobarline5 hello world*/// load the saved array:$reloaded_array = load_tabbed_file('data.txt',true);print_r($reloaded_array);// returns the array from above

相关推荐:

php 文件读取系列方法详解

php 文件类型的判断示例代码

php 文件缓存函数

以上就是php实现读写tab分割的文件的详细内容,更多请关注搞代码gaodaima其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:php实现读写tab分割的文件

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址