Home > Backend Development > PHP Tutorial > PHP implements reading and writing tab-delimited files, _PHP tutorial

PHP implements reading and writing tab-delimited files, _PHP tutorial

WBOY
Release: 2016-07-13 09:51:53
Original
826 people have browsed it

php implements reading and writing tab-delimited files,

The example in this article describes how php implements reading and writing tab-delimited files. Share it with everyone for your reference. The specific analysis is as follows:

This php code implements reading and writing tab-delimited files, including two independent functions, one for reading and one for writing, such as cvs files, etc.

//
// 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 = 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-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
Copy after login

I hope this article will be helpful to everyone’s PHP programming design.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1011258.htmlTechArticlephp implements reading and writing tab-divided files. This article describes the example of php implementing reading and writing tabs. split file. Share it with everyone for your reference. The specific analysis is as follows: This php code...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template