PHP Problem: Array -> CSV-File

Seite 1 von 1 - Forum: Coding Stuff auf overclockers.at

URL: https://www.overclockers.at/coding-stuff/php_problem_array_gt_csv-file_125143/page_1 - zur Vollversion wechseln!


gerhardtt schrieb am 21.09.2004 um 14:27

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: PHP
function 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()


watchout schrieb am 21.09.2004 um 16:02

sieht bissi umständlich aus, wie du es machst...

auch wenn du es schon gelöst hast jetzt...


gerhardtt schrieb am 21.09.2004 um 16:08

Zitat von watchout
sieht bissi umständlich aus, wie du es machst...

auch wenn du es schon gelöst hast jetzt...

wie meinen? hab im netz keine wirklich bessere lösung gefunden eine csvfile zu schreiben.

die funktion fputcsv is nicht von mir.

aber über ratschläge in sachen csv freu ich mich immer.

grüsse
gerhard


watchout schrieb am 21.09.2004 um 16:21

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' :cool:

Code: PHP
function 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;
}
edit: das filehandling wär trotzdem noch verbesserungswürdig...


gerhardtt schrieb am 21.09.2004 um 16:36

Zitat von watchout
hab' ich mir schon gedacht dass nicht alles von dir is...

Code: PHP
function 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;
}
edit: das filehandling wär trotzdem noch verbesserungswürdig...

aehm - deine $replaces regexps check ich nicht - aber sobald ein ; im text vorkommt wird der ganze text durchn "1" ersetzt. hast du dir das wirklich überlegt?

grüsse


watchout schrieb am 21.09.2004 um 16:50

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