php把txt文本轉換成數組並儲存資料庫_PHP教程

來源:互聯網
上載者:User
今天有一個工作要做就是把一個達5萬條的記錄的txt 檔案的內容要儲存到資料庫,開始想到用file_get_contents後來就看到可以用file更簡單,下面是我寫的程式.

include_once('../inc/connect.php');
class fso_getline{
var $filename ='number.txt';
var $content;
var $arr;

function read_line(){

if(file_exists($this->filename) ){

$this->content =file($this->filename);

if(is_array($this->content)){

$this->arr =$this->content;

}else{

$this->error(1);

}

}else{

$this->error(0);

}

}


function array_to_database(){

foreach( $this->arr as $temp ){

echo $temp,'
';

$this->sava_data($temp);

}

}

function sava_data($cd){

$sql ="Insert into wk_card(card_id) value('$cd')";

mysql_query($sql);


}

/*

如何使用file_get_contents()函數就要用到下面的轉換
function into_array(){

$temp =str_replace(chr(13),'|',$this->content);

$this->arr =explode('|',$temp);

}

測試輸出數組函數

function echo_array(){

print_r($this->arr);
}
*/

function error($id){

$error_array =array('file not exists','file unload');

echo $error_array[$id];

}

}
類的調用方法
$test =new fso_getline();
$test->read_line();
$test->array_to_database();

?>
最後申明一下本站原創轉載註明: www.111cn.cn/phper/php.html

http://www.bkjia.com/PHPjc/630434.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/630434.htmlTechArticle今天有一個工作要做就是把一個達5萬條的記錄的txt 檔案的內容要儲存到資料庫,開始想到用file_get_contents後來就看到可以用file更簡單,下面是...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.