php - Random linebreaks when using file() -


i'm creating flatfile database, in 1 of functions use add fields pre-existing table, use file() table, first line contains fields of table can append new field pre-existing fields

public function addfields($table_fields) {     // make sure opened connection table     if(empty($this->table_name)) throw new exception('there no connection table opened.');      // make sure table_fields array     if(is_array($table_fields) === false) throw new exception('table_fields must array');      // build data     $table_data = file($this->table_name);     $table_header = $table_data[0].self::$field_deliemeter;     $table_header .= implode(self::$field_deliemeter, $table_fields);     $table_data[0] = $table_header;     // put data in table     file_put_contents($this->table_name, implode(self::$linebreak, $table_data));     } } 

the problem that, after adding fieldname pre-existing fieldnames using this

    $table_data = file($this->table_name);     $table_header = $table_data[0].self::$field_deliemeter;     $table_header .= implode(self::$field_deliemeter, $table_fields);     $table_data[0] = $table_header; 

the first index 0 seems contain linebreak, though file() supposed remove since file implodes linebreaks

so example, if table was

name    email   password    ip  login_date somename    someemail   123 123 123 

then after running class doing this

$db = new flatdb; $fields = array("last_click"); $db->opentable('test'); $db->addfields($fields); 

it becomes this

name    email   password    ip  login_date     last_click ali ali-trixx@live.com  123 123 123 

as see, adding random linebreak after first line , have no idea why doing since file() supposed remove linebreaks

note: $field_deliemeter "\t"

does have idea why doing this?

by running print_r($table_data) before inserting, this

array ( [0] => name email   password    ip  login_date last_click [1] => somename   someemail   123 123 123 ) 

"why doing since file() supposed remove linebreaks"?

only if use file_ignore_new_lines. check file():

$table_data = file($this->table_name, file_ignore_new_lines); 

returns file in array. each element of array corresponds line in file, newline still attached. upon failure, file() returns false.

note: each line in resulting array include line ending, unless file_ignore_new_lines used, still need use rtrim() if not want line ending present.


Comments

Popular posts from this blog

PHPMotion implementation - URL based videos (Hosted on separate location) -

javascript - Using Windows Media Player as video fallback for video tag -

c# - Unity IoC Lifetime per HttpRequest for UserStore -