Citeste si scrie fisiere tsv

postat acum 10 luni de Stefanescu Mihai in categorie Snippets
//
// 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 < count($content); $x++){
        if (trim($content[$x]) != ''){
            $line = explode("\t", trim($content[$x]));
            if ($load_keys){
                $key = array_shift($line);
                $array[$key] = $line;
            }
            else { $array[] = $line; }
        }
    }
    return $array;
}
 
/*
** Example usage:
*/
 
$array = array(
    'line1'  => 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

 

Sunt un tanar programator din Bucuresti ce lucreaza in PHP/Mysql (MySqli/PDO), Laravel, CodeIgniter, MySQL, PostgreSQL, Wordpress, HTML5/CSS3, Sass, Photoshop si multe altele.
Google+ Community Facebook Group

Comentariu postate de how to download minecraft free la data de 14.05.2019
Terrific article! This is the type of info that should be shared around the web. Disgrace on the seek engines for no longer positioning this put up upper! Come on over and seek advice from my site . Thanks =)
Comentariu postate de minecraft download for free la data de 15.05.2019
With havin so much written content do you ever run into any problems of plagorism or copyright infringement? My blog has a lot of exclusive content I've either created myself or outsourced but it seems a lot of it is popping it up all over the web without my authorization. Do you know any techniques to help prevent content from being stolen? I'd definitely appreciate it.
Comentariu postate de minecraft for free la data de 16.05.2019
Wow, that's what I was seeking for, what a material! existing here at this blog, thanks admin of this site.
Comentariu postate de minecraft download for free la data de 17.05.2019
Good day! I simply want to give you a huge thumbs up for the excellent information you have got right here on this post. I am coming back to your blog for more soon.