URL: https://www.overclockers.at/coding-stuff/php_problem_array_gt_csv-file_125143/page_1 - zur Vollversion wechseln!
EDIT: hatte kein richtiges array -> der code hat eh gestimmt.
grüsse
hi,
hier ein source code um ein array in eine csv datei zu schreiben.
Problem: der String wird zwar richtig erzeugt aber beim schreiben in die datei verändert sich anscheinend irgendwas damit (wenn ich die datei im editor öffne sehe ich anstatt des deliminators ";" ein rechteck???
ich schätzmal für einen php kenner sollte das kein problem sein...
tia
gerhard
Code: PHPfunction writeArray($datei_name,$news_array) { $datei = fopen($datei_name,"w"); if ($datei) { //schreibe news_array in datei for ($i = 0; $i < count($news_array); $i++) { fputcsv($datei, $news_array[$i],';'); } } fclose($datei); } // function umwandlung text->csv function fputcsv ($fp, $array, $deliminator=';') { $line = ""; foreach($array as $val) { # remove any windows new lines, # as they interfere with the parsing at the other end $val = str_replace("\r\n", "\n", $val); # if a deliminator char, a double quote char or a newline # are in the field, add quotes if(ereg("[$deliminator\"\n\r]", $val)) { $val = '"'.str_replace('"', '""', $val).'"'; }#end if $line .= $val.$deliminator; }#end foreach # strip the last deliminator $line = substr($line, 0, (strlen($deliminator) * -1)); # add the newline $line .= "\n\r"; # we don't care if the file pointer is invalid, # let fputs take care of it return fputs($fp, $line); }#end fputcsv()
sieht bissi umständlich aus, wie du es machst...
auch wenn du es schon gelöst hast jetzt...
Zitat von watchoutsieht bissi umständlich aus, wie du es machst...
auch wenn du es schon gelöst hast jetzt...
hab' ich mir schon gedacht dass nicht alles von dir is...
hab' mir erlaubt, da ein bissi was zu verbessern... vor allem weil ichs denk ich auch mal brauchen könnt' 
edit: das filehandling wär trotzdem noch verbesserungswürdig...Code: PHPfunction writeArray($datei_name,$news_array) { $datei = fopen($datei_name,'w'); if ($datei) { # schreibe news_array in datei foreach($news_array as $news_item) { fputs($datei, return_csv($news_item,';')); } } fclose($datei); } // function to convert an array into a csv-record function return_csv ($array, $delimiter=';') { # strip any unallowed chars or add qoutes if there are any... # any double quotes must be duplicated... $unallowed = array('/"/','/^(.*(?:"|\r|\n|'.$delimiter.').*)$/'); $replaces = array('""','"\1"'); $parsed = preg_replace($unallowed,$replaces,$array); # make the line $line = implode($delimiter,$parsed)."\n\r"; return $line; }
Zitat von watchouthab' ich mir schon gedacht dass nicht alles von dir is...edit: das filehandling wär trotzdem noch verbesserungswürdig...Code: PHPfunction writeArray($datei_name,$news_array) // function to convert an array into a csv-record function return_csv ($array, $delimiter=';') { # strip any unallowed chars or add qoutes if there are any... # any double quotes must be duplicated... $unallowed = array('/"/','/^(.*(?:"|\r|\n|'.$delimiter.').*)$/'); $replaces = array('""','"\1"'); $parsed = preg_replace($unallowed,$replaces,$array); # make the line $line = implode($delimiter,$parsed)."\n\r"; return $line; }
jo, sorry is ein fehler vom forum, es werden backslashes (\) im php tag gestripped, vorm 1er ghört natürlich eins hin
overclockers.at v4.thecommunity
© all rights reserved by overclockers.at 2000-2026