Php reads and writes tab-separated files

Source: Internet
Author: User
This article describes how to read and write tab-separated php files, and related techniques for php file read/write and string operations, for more information about how to read and write tab files in php, see the example in this article. Share it with you for your reference. The specific analysis is as follows:

This php code reads and writes tab-separated files, including two independent functions, one read and one write, such as cvs files.

//// 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-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

I hope this article will help you with php programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.