aktuelle.kurse/oldies/m133/4_Modulinhalte_und_Uebungen/00-Anwendungen-Beispiele-Uebungen/PHP_Referenz/funktionsreferenz/dateisystem_funktionen/fgetcsv.html

207 lines
10 KiB
HTML
Raw Normal View History

2022-02-23 22:44:33 +01:00
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>fgetcsv</title>
<link rel="stylesheet" type="text/css" href="../../src/self.css">
<meta name="description" content="$Uuml;berpr&uuml;ft eine Zeile auf Kommaseparierte Werte">
<meta name="keywords" content="SELFPHP, PHP, Dateisystem-Funktionen, fgetcsv">
<meta name="author" content="Damir Enseleit, mail@selfphp.info">
<meta name="DC.Contributer" content="Matthias Hupp, m.hupp@selfphp3.de">
<meta name="DC.Publisher" content="Damir Enseleit, mail@selfphp.info">
<meta name="DC.Date" content="2001-08-13T12:18+01:00">
<meta name="DC.Identifier" content="http://www.selfphp3.de/funktionsreferenz/dateisystem_funktionen/fgetcsv.html">
<meta name="DC.Language" content="de">
<meta name="DC.Rights" content="../../editorial/index.html">
<meta name="SELF.Firstdate" content="2001-08-13T12:18+01:00">
<meta name="SELF.Version" content="1">
<meta name="SELF.Pagetype" content="Information">
<meta name="SELF.Path" content="SELFPHP == ../../index.php // Funktionsreferenz == ../index.php // Dateisystem-Funktionen == index.html">
</head>
<body bgcolor="#FFFFFF" text="#000000" link="#AA5522" vlink="#772200" alink="#000000">
<table cellpadding="4" cellspacing="0" border="0" width="100%">
<tr>
<td bgcolor="#FFEEDD" class="nav"><a class="an" name="top"><img src="../../src/refkap.gif" width="16" height="13" border="0" alt="Teil von"></a> <a href="../../index.html"><b>SELFPHP</b></a>/<a href="../../navigation/index.html">Navigationshilfen</a> <img src="../../src/refkap.gif" width="16" height="13" border="0" alt="Teil von"> <a href="../index.html"><b>Funktionsreferenz</b></a> <img src="../../src/refkap.gif" width="16" height="13" border="0" alt="Teil von"> <a href="index.html"><b>Dateisystem-Funktionen</b></a></td>
</tr>
</table>
<table cellpadding="4" cellspacing="0" border="0" width="100%">
<tr>
<td bgcolor="#EEEEEE" class="doc" width="110"><img src="../../src/logo.gif" width="106" height="109" border="0" alt="SEL.html"></td>
<td bgcolor="#EEEEEE" class="doc" valign="bottom" width="100%">
<h2>fgetcsv</h2></td>
</tr>
<tr>
<td bgcolor="#EEEEEE" class="doc" valign="top" align="center"> <img src="../../src/dokx.gif" width="30" height="20" vspace="6" border="0" alt="Informationsseite">
</td>
<td bgcolor="#FFFFFF" valign="top" nowrap colspan="2">
<p> <img src="../../src/down.gif" width="14" height="10" border="0" alt="nach unten">&nbsp;<a href="#befehl"><b>Befehl</b></a><br>
<img src="../../src/down.gif" width="14" height="10" border="0" alt="nach unten">&nbsp;<a href="#version"><b>Version</b></a><br>
<img src="../../src/down.gif" width="14" height="10" border="0" alt="nach unten">&nbsp;<a href="#beschreibung"><b>Beschreibung</b></a><br>
<img src="../../src/down.gif" width="14" height="10" border="0" alt="nach unten">&nbsp;<a href="#beispiel"><b>Beispiel</b></a><br>
<img src="../../src/down.gif" width="14" height="10" border="0" alt="nach unten">&nbsp;<a href="#ausgabe"><b>Ausgabe</b></a><br>
</p>
</td>
</tr>
<tr>
<td colspan=3 bgcolor="#EEEEEE" class="doc"><a href="#bottom"><img src="../../src/down.gif" width="14" height="10" border="0" alt="nach unten"></a>&#160;</td>
</tr>
</table>
<h2 class="Sh2"><a class="an" name="befehl">Befehl</a></h2>
<table width="100%" cellpadding=10><tr>
<td class="xmpcode" bgcolor="#E4EEFF">array <b>fgetcsv</b>(int fp, int length
[, string Trennzeichen (Delimiter)])</td>
</tr></table>
<p>&nbsp;</p>
<table bgcolor="#EEEEEE" class="doc" width="100%"><tr><td>
<a href="#top"><img src="../../src/up.gif" width="14" height="10" border="0" alt="nach oben"></a><a href="#bottom"><img src="../../src/down.gif" width="14" height="10" border="0" alt="nach unten"></a>&#160;
</td></tr></table>
<h2 class="Sh2"><a class="an" name="version">Version</a></h2>
<table width="100%" cellpadding=10 height="32">
<tr>
<td class="xmpcode" bgcolor="#E4EEFF" height="20">
<p> PHP 3 &gt;= 3.0.8, PHP 4 &gt;= 4.0b1</p>
</td>
</tr></table>
<p>&nbsp;</p>
<table bgcolor="#EEEEEE" class="doc" width="100%"><tr><td>
<a href="#top"><img src="../../src/up.gif" width="14" height="10" border="0" alt="nach oben"></a><a href="#bottom"><img src="../../src/down.gif" width="14" height="10" border="0" alt="nach unten"></a>&#160;
</td></tr></table>
<h2 class="Sh2"><a class="an" name="beschreibung">Beschreibung</a></h2>
<table width="100%" cellpadding=10 height="243">
<tr>
<td class="xmpcode" bgcolor="#E4EEFF" height="57">
<p>Mit fgetcsv() kann man aus einer CSV-Datei (fp) eine Zeile auslesen und
den<br>
Inhalt der Zeile anhand eines Trennzeichens (Delimiter) in seine Bestandteile<br>
zerlegen lassen. Diese Bestandteile gibt die Funktion als Array zur&uuml;ck.<br>
Sollten Sie optionalen Parameter Delimiter weglassen, so wird der voreingestellte<br>
Wert genutzt - dieses ist ein Komma.<br>
Der zweite Parameter (length) muss gr&ouml;&szlig;er sein als die l&auml;ngste
Zeile in der Datei,<br>
da sonst die Funktion fgetcsv() das Ende der Zeile nicht finden wird.<br>
Falls bereits die komplette Datei durchlaufen wurde, wird als Wert false<br>
zur&uuml;ckgegeben. Sollten leere Zeilen in der Datei vorkommen, so wird
das nicht<br>
als Fehler interpretiert - Sie bekommen in diesem Fall ein Array mit einem
leeren<br>
Feld zur&uuml;ck.</p>
<p>Beachten Sie bitte, dass Sie als Trennzeichen nur ein einzelnes Zeichen
nehmen<br>
sollten, da es sonst zu ungewollten Nebeneffekten kommen kann. Betrachten
Sie<br>
dazu folgendes Beispiel:<br>
Die gelesene Zeile hat den Inhalt: &quot;01.08.2001##62.159.232.250##www.selfphp3.de&quot;<br>
Wenn Sie diese Zeile mit<br>
fgetcsv($fp,500,&quot;##&quot;) <br>
lesen, erhalten Sie ein Array mit 5 Feldern, da nur eine Raute als Trennzeichen<br>
ber&uuml;cksichtigt wird.</p>
<p>Beachten Sie bitte, dass es sich bei dem Dateizeiger fp um einen g&uuml;ltigen
Zeiger<br>
auf eine offene Datei handeln muss, der mit fopen(), fsockopen() oder
popen()<br>
erzeugt wurde.</p>
</td>
</tr></table>
<p>&nbsp;</p>
<table bgcolor="#EEEEEE" class="doc" width="100%"><tr><td>
<a href="#top"><img src="../../src/up.gif" width="14" height="10" border="0" alt="nach oben"></a><a href="#bottom"><img src="../../src/down.gif" width="14" height="10" border="0" alt="nach unten"></a>&#160;
</td></tr></table>
<h2 class="Sh2"><a class="an" name="beispiel">Beispiel</a></h2>
<table width="100%" cellpadding=10 height="62">
<tr>
<td class="xmpcode" bgcolor="#E4EEFF">
<pre>//Inhalt der Datei statistik.csv:
//01.08.2001#62.159.232.250#www.selfphp3.de<br>//02.08.2001#212.82.34.222#www.selfphp4.de<br>//02.08.2001#http://suchen.abacho.de#62.159.232.250<br>//03.08.2001#http://www.fireball.de#212.185.44.15
&lt;?PHP<br>$statistik = &quot;statistik.csv&quot;;<br>$fp = fopen($statistik,&quot;r&quot;);<br>while($zeile = fgetcsv($fp,500,&quot;#&quot;)){<br> $y++;<br> echo &quot;&lt;b&gt;Besucher $y:&lt;/b&gt;&quot;;<br> echo &quot;&lt;br&gt;Datum/HTTP_REFERER/REMOTE_ADDR&lt;br&gt;&quot;; <br> for($x=0;$x&lt;count($zeile);$x++){<br> echo &quot;$zeile[$x]&quot;;<br> echo &quot;&lt;br&gt;&quot;;<br> }<br> echo &quot;&lt;br&gt;&quot;;<br>}<br>fclose($fp);<br>?&gt;</pre>
</td>
</tr></table>
<p>&nbsp;</p>
<table bgcolor="#EEEEEE" class="doc" width="100%"><tr><td>
<a href="#top"><img src="../../src/up.gif" width="14" height="10" border="0" alt="nach oben"></a><a href="#bottom"><img src="../../src/down.gif" width="14" height="10" border="0" alt="nach unten"></a>&#160;
</td></tr></table>
<h2 class="Sh2"><a class="an" name="ausgabe">Ausgabe</a></h2>
<table width="100%" cellpadding=10 height="34">
<tr>
<td class="xmpcode" bgcolor="#E4EEFF" height="57">
<p><b>Besucher 1:</b><br>
Datum / HTTP_REFERER / REMOTE_ADDR<br>
01.08.2001<br>
62.159.232.250<br>
www.selfphp3.de</p>
<p><b>Besucher 2:</b><br>
Datum / HTTP_REFERER / REMOTE_ADDR<br>
02.08.2001<br>
212.82.34.222<br>
www.selfphp4.de</p>
<p><b>Besucher 3:</b><br>
Datum / HTTP_REFERER / REMOTE_ADDR<br>
02.08.2001<br>
http://suchen.abacho.de<br>
62.159.232.250</p>
<p><b>Besucher 4:</b><br>
Datum / HTTP_REFERER / REMOTE_ADDR<br>
03.08.2001<br>
http://www.fireball.de<br>
212.185.44.15</p>
</td>
</tr></table>
<p>&nbsp;</p>
<table cellpadding="4" cellspacing="0" border="0" width="100%">
<tr><td colspan=2 bgcolor="#EEEEEE" class="doc">
<a href="#top"><img src="../../src/up.gif" width="14" height="10" border="0" alt="nach oben"></a>
</td></tr>
<tr>
<td bgcolor="#EEEEEE" class="doc" align="right" width="110">weiter:<a href="fgets.html"><img src="../../src/next.gif" width="10" height="10" border="0" hspace="10" alt="weiter"></a></td>
<td bgcolor="#FFFFFF"><img src="../../src/dok.gif" width="15" height="10" border="0" alt="Seite">
<a href="fgets.html"><b>fgets</b></a> </td>
</tr>
<tr>
<td bgcolor="#EEEEEE" class="doc" align="right" width="110">zur&uuml;ck:<a href="fgetc.html"><img src="../../src/prev.gif" width="10" height="10" border="0" hspace="10" alt="zur&uuml;ck"></a></td>
<td bgcolor="#FFFFFF" width="100%"><img src="../../src/dok.gif" width="15" height="10" border="0" alt="Seite">
<a href="fgetc.html"><b>fgetc</b></a> </td>
</tr>
<tr><td colspan=2 bgcolor="#EEEEEE" class="doc">&#160;</td></tr>
</table><table cellpadding="4" cellspacing="0" border="0" width="100%">
<tr>
<td bgcolor="#FFEEDD" class="nav"><a class="an" name="bottom"><img src="../../src/refkap.gif" width="16" height="13" border="0" alt="Teil von"></a>
<a href="../../index.html"><b>SELFPHP</b></a>/<a href="../../navigation/index.html">Navigationshilfen</a> <img src="../../src/refkap.gif" width="16" height="13" border="0" alt="Teil von">
<a href="../index.html"><b>Funktionsreferenz</b></a> <img src="../../src/refkap.gif" width="16" height="13" border="0" alt="Teil von">
<a href="index.html"><b>Dateisystem-Funktionen</b></a></td>
</tr>
</table>
<p><a class="an" name="bottom">&copy; 2001, 2002, 2003</A> <img src="../../src/mail.gif" width="15" height="10" border="0" alt="E-Mail">&nbsp;<a href="mailto:mail@selfphp.info">Damir Enseleit, mail@selfphp.info</a></p>
</body>
</html>