溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

php怎么生成txt文件標(biāo)題及內(nèi)容

發(fā)布時(shí)間:2021-09-08 16:43:52 來(lái)源:億速云 閱讀:176 作者:chen 欄目:開發(fā)技術(shù)

本篇內(nèi)容主要講解“php怎么生成txt文件標(biāo)題及內(nèi)容”,感興趣的朋友不妨來(lái)看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來(lái)帶大家學(xué)習(xí)“php怎么生成txt文件標(biāo)題及內(nèi)容”吧!

復(fù)制代碼 代碼如下:


<?php
/**
*1.前幾天一哥們工作中他們領(lǐng)導(dǎo)讓他寫一個(gè)上生成文件的類:生成文件,文件類型支持:txt、html、csv、pdf、doc(或者docx)。
*
*2.生成的內(nèi)容是一張表格(像html中的table),參數(shù)為:生成文件的類型、生成內(nèi)容的標(biāo)題(數(shù)組),生成內(nèi)容(數(shù)組,和標(biāo)題相對(duì)應(yīng))。
*/
/*************************************************
* class name:createFile
* description:create different type files
* author:fenghuo
* date:2013-11-12
************************************************/
/**
*3.我利用晚上的時(shí)間幫他就整理了一個(gè)生成txt的文件類.
***/
class createFile{
public $file_type;
public $file_name;
public $file_dir;
/**
* 構(gòu)造函數(shù):初始化生成文件的目錄
*/
public function __construct($file_dir){
$this->file_dir = $file_dir;
}
/**
* 生成文件的入口函數(shù)
* @string $file_name 文件名
* @string $file_type 文件類型
* @array $title 生成內(nèi)容的標(biāo)題行
* @array $data 生成內(nèi)容
*/
public function create_file($file_name,$file_type,$title,$data){
if(empty($data)){
return false;
}
if(!empty($title)){
if(count($title) != count($data[0])){
return false;
}
}
if($file_name == ""){
$file_name = $this->file_name;

}
if($file_type == ""){
$file_type = $this->file_type;
}
$fun = 'mk_'.$file_type;
# 測(cè)試點(diǎn)
echo $fun,'--------------<br/>';
if( method_exists( $this,$fun))
{
$file = $file_name.".".$file_type;
$this -> $fun ($file,$title,$data);
return true;
}else{
return "NO!";
}
}
/**
*生成txt類型文件
*@string $file 文件名
*@array $title 標(biāo)題
*@array $data 內(nèi)容
*/
public function mk_txt($file,$title,$data){
$string = "";
if(!empty($title)){
for( $i = 0;$i < count( $title ); $i++ ){
$string .= ' '. mb_convert_encoding($title[$i],'GBK',"UTF-8");
}
$string .="\r\n";
}
foreach ( $data as $key =>$var)
{
for( $i = 0; $i < count($data[$key]); $i++ ){
$string .= ' '. mb_convert_encoding($data[$key][$i],'GBK',"UTF-8");
}
$string .="\r\n";
}
# 測(cè)試點(diǎn)
echo $this->file_dir.$file,'-----123---------<br/>';
$fp = fopen($this->file_dir.$file, "a+");
fwrite($fp,$string);
fclose($fp);
return true;
}


}

//**************************************
//測(cè)試
$dir ='E:\dev\ ';
$file_name = "test";
$file_type = "txt";
$title = array("name","sex","age");
$data[] = array("tom","boy",20);
$data[] = array("perry","girl",20);
$file = new createFile($dir);
$flag = $file-> create_file($file_name,$file_type,$title,$data);
if($flag == true){
echo "生成成功";
}else{
echo "生成失敗";
}

?>

到此,相信大家對(duì)“php怎么生成txt文件標(biāo)題及內(nèi)容”有了更深的了解,不妨來(lái)實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

php
AI