PHP csv mit utf8

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Gonzo2000
Forum Newbie
Posts: 1
Joined: Thu May 14, 2009 1:16 am

PHP csv mit utf8

Post by Gonzo2000 »

Code: Select all

<?php
function utf_8_fprintf ($format) {
      $args = func_get_args();
 
        for ($i = 1; $i < count($args); $i++) {
                $args [$i] = iconv('UTF-8', 'ISO-8859-2', $args [$i]);
              }
       
        return iconv('ISO-8859-2', 'UTF-8', call_user_func_array('fprintf', $args));
}
 
$readfile = fopen ("34k.csv",'r');
$writefile = fopen("output.csv","w+");
 
$data=fgetcsv($readfile,1000,",");
fwrite($writefile, "# $data[0] $data[3] ");
fwrite($writefile, date("d M Y"));
fwrite($writefile,"\n");
fgetcsv($readfile,1000,",");
 
while(($data=fgetcsv($readfile,1000,","))!=FALSE) {
    fprintf($writefile,"%-5s,",$data[0]);
    utf_8_fprintf($writefile,"%-15s,",$data[1]);
    utf_8_fprintf($writefile,"%-15s,",$data[2]);
 
    if(( $data[3] != "RK" ) or ($data[4]!="")) {
        fprintf($writefile,"%-5s,","-RK");
        if($data[3]=="isl.")
            fprintf($writefile,"%-5s","RISL");
        else
            fprintf($writefile,"%-5s","");
    }
    else
        fprintf($writefile,"%-5s,%-5s","","");
 
    if($data[7]!="")
        utf_8_fprintf($writefile,"# %s",$data[7]);
 
    fprintf($writefile,"\n");
}
 
fclose($readfile);
fclose($writefile);
?>
 
Last edited by Benjamin on Thu May 14, 2009 1:24 am, edited 1 time in total.
Reason: Added [code=php] tags.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: PHP csv mit utf8

Post by Benjamin »

Why did you post this?
Post Reply