Heim  >  Artikel  >  Backend-Entwicklung  >  php把txt文本转换成数组并保存数据库_PHP教程

php把txt文本转换成数组并保存数据库_PHP教程

WBOY
WBOYOriginal
2016-07-13 17:07:251239Durchsuche

今天有一个工作要做就是把一个达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

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/630434.htmlTechArticle今天有一个工作要做就是把一个达5万条的记录的txt 文件的内容要保存到数据库,开始想到用file_get_contents后来就看到可以用file更简单,下面是...
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn