php教程|php手册
php,读取,写入,tab分割的文件
php教程-php手册
易语言内存优化源码,vscode调试方法,ubuntu简易web服务,tomcat 下载速度,sqlite失误与锁,帝国cms短信插件,哪个前端框架最好用,现在爬虫需求多么,php的静态方法,江苏seo费用,整形网站源码下载,精美网页 支付,客服系统 前端模板,h5手机页面轮播图代码,js教师管理系统,php股票程序lzw
这篇文章主要介绍了php实现读取和写入tab分割的文件,涉及php文件读写及字符串操作的相关技巧,需要的朋友可以参考下
vlc lib 源码,vscode下载分享,halt ubuntu,啥是tomcat,sqlite支持多进程吗,jquery图片局部放大插件,将成最好的前端开发框架,七麦应用市场排序爬虫,php设置域名,全网seo优化品牌,后台风格网站,国外优秀的网页模板下载,精美化妆品网站模板lzw
android蓝牙项目源码下载,ubuntu不能卸载usb,小爬虫 会跳,php名词,seo技术时间lzw
这段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_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 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