<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Andreas Droesch &#187; PHP</title>
	<atom:link href="http://andreas.droesch.de/category/code-snippets/php-snippets/feed/" rel="self" type="application/rss+xml" />
	<link>http://andreas.droesch.de</link>
	<description>Weblog und Portfolio...</description>
	<lastBuildDate>Fri, 23 Oct 2009 21:12:14 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.5</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>PHP: Dateien und Ordner auflisten</title>
		<link>http://andreas.droesch.de/2009/10/php-dateien-und-ordner-auflisten/</link>
		<comments>http://andreas.droesch.de/2009/10/php-dateien-und-ordner-auflisten/#comments</comments>
		<pubDate>Fri, 23 Oct 2009 21:12:14 +0000</pubDate>
		<dc:creator>Andreas</dc:creator>
				<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Dateien]]></category>
		<category><![CDATA[Dateityp]]></category>
		<category><![CDATA[Ordner]]></category>

		<guid isPermaLink="false">http://andreas.droesch.de/?p=602</guid>
		<description><![CDATA[Diese Funktion liest die Namen aller Dateien und Ordner in einem Verzeichnis ein und gibt diese in einem Array zurück, welches danach etwa mit einer Schleife aufgelistet werden kann.
Die Dateien können zusätzlich auf bestimmte Typen beschränkt und die Auflistung von Ordner deaktiviert werden.

&#60;?php//&#160;Dateien&#160;und&#160;Ordner&#160;aus&#160;einem&#160;Verzeichnis&#160;ermittelnfunction&#160;list_dir($path,&#160;$extensions&#160;=&#160;'',&#160;$list_folder&#160;=&#160;false)&#160;{&#160;&#160;//&#160;Prüfen&#160;ob&#160;das&#160;Verzeichnis&#160;existiert&#160;&#160;if&#160;(!file_exists($path))&#160;{&#160;&#160;&#160;&#160;return&#160;array();&#160;&#160;}&#160;&#160;//&#160;Liste&#160;erlaubter&#160;Erweiterungen&#160;erstellen&#160;&#160;if&#160;(!empty($extensions))&#160;{&#160;&#160;&#160;&#160;$extensions&#160;=&#160;explode(',',&#160;$extensions);&#160;&#160;}&#160;&#160;//&#160;Pfad&#160;um&#160;Backslash&#160;am&#160;Schluss&#160;ergänzen&#160;&#160;$path&#160;=&#160;rtrim($path,&#160;'/')&#160;.&#160;'/';&#160;&#160;//&#160;Ordner&#160;öffnen&#160;und&#160;alle&#160;Einträge&#160;durchlaufen&#160;&#160;$dh&#160;=&#160;opendir($path);&#160;&#160;$list&#160;=&#160;array();&#160;&#160;while&#160;($file&#160;=&#160;readdir($dh))&#160;{&#160;&#160;&#160;&#160;$is_dir&#160;=&#160;is_dir($path&#160;.&#160;$file);&#160;&#160;&#160;&#160;//&#160;Ordner&#160;überspringen,&#160;falls&#160;nicht&#160;erwünscht&#160;&#160;&#160;&#160;if&#160;((!$list_folder&#160;&#38;&#38;&#160;$is_dir)&#160;&#124;&#124;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;//&#160;Sprungmarken&#160;überspringen&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;($file&#160;==&#160;'.'&#160;&#124;&#124;&#160;$file&#160;==&#160;'..')&#160;&#124;&#124;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;//&#160;Unerwünschte&#160;Dateitypen&#160;überspringen&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;($extensions&#160;&#38;&#38;&#160;!$is_dir&#160;&#38;&#38;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;!in_array(strtolower(substr(strrchr($file,&#160;'.'),&#160;1)),&#160;$extensions)))&#160;{&#160;&#160;&#160;&#160;&#160;&#160;continue;&#160;&#160;&#160;&#160;}&#160;&#160;&#160;&#160;//&#160;Datei&#160;zur&#160;Liste&#160;hinzufügen&#160;&#160;&#160;&#160;$list[]&#160;=&#160;$file;&#160;&#160;}&#160;&#160;//&#160;Ordner&#160;schließen&#160;und&#160;Liste&#160;zurückgeben&#160;&#160;closedir($dh);&#160;&#160;return&#160;$list;}?&#62;


Als Parameter erwartet die Funktion den Pfad des auszulesenden Verzeichnisses, relativ zum Script. [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">Diese Funktion liest die Namen aller Dateien und Ordner in einem Verzeichnis ein und gibt diese in einem Array zurück, welches danach etwa mit einer Schleife aufgelistet werden kann.</p>
<p style="text-align: justify;">Die Dateien können zusätzlich auf bestimmte Typen beschränkt und die Auflistung von Ordner deaktiviert werden.</p>
<p><span id="more-602"></span><div class="wpsp_code_div"><code><span style="color: #000000">
<span style="color: #007700">&lt;?</span><span style="color: #0000BB">php<br /><br /></span><span style="color: #FF9900">//&nbsp;Dateien&nbsp;und&nbsp;Ordner&nbsp;aus&nbsp;einem&nbsp;Verzeichnis&nbsp;ermitteln<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">list_dir</span><span style="color: #007700">(</span><span style="color: #0000BB">$path</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$extensions&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">''</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$list_folder&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">false</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;</span><span style="color: #FF9900">//&nbsp;Prüfen&nbsp;ob&nbsp;das&nbsp;Verzeichnis&nbsp;existiert<br />&nbsp;&nbsp;</span><span style="color: #007700">if&nbsp;(!</span><span style="color: #0000BB">file_exists</span><span style="color: #007700">(</span><span style="color: #0000BB">$path</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;array();<br />&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;</span><span style="color: #FF9900">//&nbsp;Liste&nbsp;erlaubter&nbsp;Erweiterungen&nbsp;erstellen<br />&nbsp;&nbsp;</span><span style="color: #007700">if&nbsp;(!empty(</span><span style="color: #0000BB">$extensions</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$extensions&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">explode</span><span style="color: #007700">(</span><span style="color: #DD0000">','</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$extensions</span><span style="color: #007700">);<br />&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;</span><span style="color: #FF9900">//&nbsp;Pfad&nbsp;um&nbsp;Backslash&nbsp;am&nbsp;Schluss&nbsp;ergänzen<br />&nbsp;&nbsp;</span><span style="color: #0000BB">$path&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">rtrim</span><span style="color: #007700">(</span><span style="color: #0000BB">$path</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'/'</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">'/'</span><span style="color: #007700">;<br /><br />&nbsp;&nbsp;</span><span style="color: #FF9900">//&nbsp;Ordner&nbsp;öffnen&nbsp;und&nbsp;alle&nbsp;Einträge&nbsp;durchlaufen<br />&nbsp;&nbsp;</span><span style="color: #0000BB">$dh&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">opendir</span><span style="color: #007700">(</span><span style="color: #0000BB">$path</span><span style="color: #007700">);<br />&nbsp;&nbsp;</span><span style="color: #0000BB">$list&nbsp;</span><span style="color: #007700">=&nbsp;array();<br />&nbsp;&nbsp;while&nbsp;(</span><span style="color: #0000BB">$file&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">readdir</span><span style="color: #007700">(</span><span style="color: #0000BB">$dh</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$is_dir&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">is_dir</span><span style="color: #007700">(</span><span style="color: #0000BB">$path&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$file</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF9900">//&nbsp;Ordner&nbsp;überspringen,&nbsp;falls&nbsp;nicht&nbsp;erwünscht<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">if&nbsp;((!</span><span style="color: #0000BB">$list_folder&nbsp;</span><span style="color: #007700">&amp;&amp;&nbsp;</span><span style="color: #0000BB">$is_dir</span><span style="color: #007700">)&nbsp;||<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF9900">//&nbsp;Sprungmarken&nbsp;überspringen<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">(</span><span style="color: #0000BB">$file&nbsp;</span><span style="color: #007700">==&nbsp;</span><span style="color: #DD0000">'.'&nbsp;</span><span style="color: #007700">||&nbsp;</span><span style="color: #0000BB">$file&nbsp;</span><span style="color: #007700">==&nbsp;</span><span style="color: #DD0000">'..'</span><span style="color: #007700">)&nbsp;||<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF9900">//&nbsp;Unerwünschte&nbsp;Dateitypen&nbsp;überspringen<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">(</span><span style="color: #0000BB">$extensions&nbsp;</span><span style="color: #007700">&amp;&amp;&nbsp;!</span><span style="color: #0000BB">$is_dir&nbsp;</span><span style="color: #007700">&amp;&amp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;!</span><span style="color: #0000BB">in_array</span><span style="color: #007700">(</span><span style="color: #0000BB">strtolower</span><span style="color: #007700">(</span><span style="color: #0000BB">substr</span><span style="color: #007700">(</span><span style="color: #0000BB">strrchr</span><span style="color: #007700">(</span><span style="color: #0000BB">$file</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'.'</span><span style="color: #007700">),&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">)),&nbsp;</span><span style="color: #0000BB">$extensions</span><span style="color: #007700">)))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;continue;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF9900">//&nbsp;Datei&nbsp;zur&nbsp;Liste&nbsp;hinzufügen<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$list</span><span style="color: #007700">[]&nbsp;=&nbsp;</span><span style="color: #0000BB">$file</span><span style="color: #007700">;<br />&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;</span><span style="color: #FF9900">//&nbsp;Ordner&nbsp;schließen&nbsp;und&nbsp;Liste&nbsp;zurückgeben<br />&nbsp;&nbsp;</span><span style="color: #0000BB">closedir</span><span style="color: #007700">(</span><span style="color: #0000BB">$dh</span><span style="color: #007700">);<br />&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">$list</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
<p style="text-align: justify;">Als Parameter erwartet die Funktion den Pfad des auszulesenden Verzeichnisses, relativ zum Script. Optional kann als zweiter Parameter eine Liste zugelassener Dateierweiterungen angegeben werden (einzelne Erweiterungen durch ein Komma trennen). Der dritte Parameter ist ebenfalls optional und aktiviert die Auflistung von Ordnern.</p>
<p style="text-align: justify;">Nachfolgend noch ein paar Beispielaufrufe mit verschiedenen Optionen:</p>
<div class="wpsp_code_div"><code><span style="color: #000000">
<span style="color: #007700">&lt;?</span><span style="color: #0000BB">php<br /><br /></span><span style="color: #FF9900">//&nbsp;Beispielaufruf&nbsp;1:&nbsp;Alle&nbsp;Dateien<br /></span><span style="color: #0000BB">$path&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'uploads'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$files&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">list_dir</span><span style="color: #007700">(</span><span style="color: #0000BB">$path</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #DD0000">'&lt;ul&gt;'</span><span style="color: #007700">;<br />foreach&nbsp;(</span><span style="color: #0000BB">$files&nbsp;</span><span style="color: #007700">as&nbsp;</span><span style="color: #0000BB">$file</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">'&lt;li&gt;'&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$file&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #DD0000">'&lt;/li&gt;'</span><span style="color: #007700">;<br />}<br />echo&nbsp;</span><span style="color: #DD0000">'&lt;/ul&gt;'</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF9900">//&nbsp;Beispielaufruf&nbsp;2:&nbsp;Alle&nbsp;Dateien&nbsp;und&nbsp;Ordner<br /></span><span style="color: #0000BB">$files&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">list_dir</span><span style="color: #007700">(</span><span style="color: #0000BB">$path</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">''</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">true</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF9900">//&nbsp;Beispielaufruf&nbsp;3:&nbsp;Nur&nbsp;HTML&nbsp;und&nbsp;PHP&nbsp;Dateien<br /></span><span style="color: #0000BB">$files&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">list_dir</span><span style="color: #007700">(</span><span style="color: #0000BB">$path</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'html,php'</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF9900">//&nbsp;Beispielaufruf&nbsp;4:&nbsp;Nur&nbsp;HTML&nbsp;und&nbsp;PHP&nbsp;Dateien,&nbsp;sowie&nbsp;Ordner<br /></span><span style="color: #0000BB">$files&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">list_dir</span><span style="color: #007700">(</span><span style="color: #0000BB">$path</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'html,php'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">true</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
]]></content:encoded>
			<wfw:commentRss>http://andreas.droesch.de/2009/10/php-dateien-und-ordner-auflisten/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP: Monatskalender (per Table)</title>
		<link>http://andreas.droesch.de/2009/10/php-monatskalender-per-table/</link>
		<comments>http://andreas.droesch.de/2009/10/php-monatskalender-per-table/#comments</comments>
		<pubDate>Thu, 08 Oct 2009 17:56:47 +0000</pubDate>
		<dc:creator>Andreas</dc:creator>
				<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Beispiel]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Datum]]></category>
		<category><![CDATA[Kalender]]></category>

		<guid isPermaLink="false">http://andreas.droesch.de/?p=505</guid>
		<description><![CDATA[In diesem kleinen Beispiel geht es um die Ausgabe aller Tage eines Monats als Kalenderansicht in einer Tabelle. Die spätere Ausgabe würde wie folgt aussehen:



Der Funktion kann optional ein bestimmter Monat mit Jahresangabe als Parameter übergeben werden, sonst wird der aktuelle Monat dargestellt:

&#60;?php//&#160;Kalender&#160;eines&#160;Monats&#160;erzeugen&#160;(per&#160;Table)function&#160;show_calendar($month&#160;=&#160;0,&#160;$year&#160;=&#160;0)&#160;{&#160;&#160;//&#160;Timestamp&#160;für&#160;den&#160;gewählten&#160;Monat&#160;erzeugen,&#160;falls&#160;kein&#160;Datum&#160;angegeben&#160;wurde,&#160;&#160;//&#160;dann&#160;das&#160;aktuelle&#160;nehmen.&#160;&#160;$month_timestamp&#160;=&#160;($month&#160;==&#160;0&#160;?&#160;time()&#160;:&#160;mktime(12,&#160;0,&#160;0,&#160;$month,&#160;1,&#160;$year));&#160;&#160;//&#160;Kopf&#160;der&#160;Tabelle&#160;&#160;$output&#160;=&#160;'&#60;table&#160;style="text-align:center"&#62;'&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;.&#160;'&#60;tr&#160;style="font-weight:bold"&#62;'&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;.&#160;'&#60;td&#62;Mo&#60;/td&#62;&#60;td&#62;Di&#60;/td&#62;&#60;td&#62;Mi&#60;/td&#62;&#60;td&#62;Do&#60;/td&#62;&#60;td&#62;Fr&#60;/td&#62;'&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;.&#160;'&#60;td&#62;Sa&#60;/td&#62;&#60;td&#62;So&#60;/td&#62;'&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;.&#160;'&#60;/tr&#62;'&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;.&#160;'&#60;tr&#62;';&#160;&#160;//&#160;Ersten&#160;Wochentag&#160;des&#160;Monats&#160;ermitteln&#160;und&#160;bis&#160;dahin&#160;Leerzellen&#160;hinzufügen&#160;&#160;$first_day&#160;=&#160;date('w',&#160;$month_timestamp);&#160;&#160;$fill_count&#160;=&#160;($first_day&#160;==&#160;0&#160;?&#160;6&#160;:&#160;$first_day&#160;-&#160;1);&#160;&#160;$output&#160;.=&#160;str_repeat('&#60;td&#62;&#38;nbsp;&#60;/td&#62;',&#160;$fill_count);&#160;&#160;//&#160;Monatstage&#160;auflisten&#160;&#160;$day_count&#160;=&#160;date('t',&#160;$month_timestamp);&#160;&#160;for&#160;($i&#160;=&#160;1;&#160;$i&#160;&#60;=&#160;$day_count;&#160;$i++)&#160;{&#160;&#160;&#160;&#160;//&#160;Tageszelle&#160;einfügen&#160;&#160;&#160;&#160;$output&#160;.=&#160;'&#60;td&#62;'&#160;.&#160;$i&#160;.&#160;'&#60;/td&#62;';&#160;&#160;&#160;&#160;//&#160;Neue&#160;Zeile&#160;nach&#160;7&#160;Spalten&#160;&#160;&#160;&#160;if&#160;(++$fill_count&#160;%&#160;7&#160;==&#160;0)&#160;{&#160;&#160;&#160;&#160;&#160;&#160;$output&#160;.=&#160;'&#60;/tr&#62;&#60;tr&#62;';&#160;&#160;&#160;&#160;}&#160;&#160;}&#160;&#160;//&#160;Letzte&#160;Reihe&#160;mit&#160;Leerzellen&#160;auffüllen&#160;&#160;while&#160;($fill_count++&#160;%&#160;7&#160;!=&#160;0)&#160;{&#160;&#160;&#160;&#160;$output&#160;.=&#160;'&#60;td&#62;&#38;nbsp;&#60;/td&#62;';&#160;&#160;}&#160;&#160;//&#160;Tabelle&#160;schließen&#160;&#160;$output&#160;.=&#160;'&#60;/tr&#62;'&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;.&#160;'&#60;/table&#62;';&#160;&#160;//&#160;Kalender&#160;zurückgeben&#160;&#160;return&#160;$output;}?&#62;


Aufgerufen werden kann die Funktion wie folgt:

&#60;?php//&#160;Beispielaufruf&#160;1:&#160;Aktueller&#160;Monatecho&#160;show_calendar();//&#160;Beispielaufruf&#160;2:&#160;Vorgegebener&#160;Monatecho&#160;show_calendar(1,&#160;2009)?&#62;


]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">In diesem kleinen Beispiel geht es um die Ausgabe aller Tage eines Monats als Kalenderansicht in einer Tabelle. Die spätere Ausgabe würde wie folgt aussehen:</p>
<p style="text-align:center"><img style="margin:10px 0" title="Monatskalender" src="http://andreas.droesch.de/files/codesnippets/php_monatskalender/calendar_table.gif" alt="Monatskalender" width="153" height="131" /></p>
<p><span id="more-505"></span></p>
<p style="text-align: justify;">
Der Funktion kann optional ein bestimmter Monat mit Jahresangabe als Parameter übergeben werden, sonst wird der aktuelle Monat dargestellt:</p>
<div class="wpsp_code_div"><code><span style="color: #000000">
<span style="color: #007700">&lt;?</span><span style="color: #0000BB">php<br /><br /></span><span style="color: #FF9900">//&nbsp;Kalender&nbsp;eines&nbsp;Monats&nbsp;erzeugen&nbsp;(per&nbsp;Table)<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">show_calendar</span><span style="color: #007700">(</span><span style="color: #0000BB">$month&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$year&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;</span><span style="color: #FF9900">//&nbsp;Timestamp&nbsp;für&nbsp;den&nbsp;gewählten&nbsp;Monat&nbsp;erzeugen,&nbsp;falls&nbsp;kein&nbsp;Datum&nbsp;angegeben&nbsp;wurde,<br />&nbsp;&nbsp;//&nbsp;dann&nbsp;das&nbsp;aktuelle&nbsp;nehmen.<br />&nbsp;&nbsp;</span><span style="color: #0000BB">$month_timestamp&nbsp;</span><span style="color: #007700">=&nbsp;(</span><span style="color: #0000BB">$month&nbsp;</span><span style="color: #007700">==&nbsp;</span><span style="color: #0000BB">0&nbsp;</span><span style="color: #007700">?&nbsp;</span><span style="color: #0000BB">time</span><span style="color: #007700">()&nbsp;:&nbsp;</span><span style="color: #0000BB">mktime</span><span style="color: #007700">(</span><span style="color: #0000BB">12</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$month</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$year</span><span style="color: #007700">));<br /><br />&nbsp;&nbsp;</span><span style="color: #FF9900">//&nbsp;Kopf&nbsp;der&nbsp;Tabelle<br />&nbsp;&nbsp;</span><span style="color: #0000BB">$output&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'&lt;table&nbsp;style="text-align:center"&gt;'<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #DD0000">'&lt;tr&nbsp;style="font-weight:bold"&gt;'<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #DD0000">'&lt;td&gt;Mo&lt;/td&gt;&lt;td&gt;Di&lt;/td&gt;&lt;td&gt;Mi&lt;/td&gt;&lt;td&gt;Do&lt;/td&gt;&lt;td&gt;Fr&lt;/td&gt;'<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #DD0000">'&lt;td&gt;Sa&lt;/td&gt;&lt;td&gt;So&lt;/td&gt;'<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #DD0000">'&lt;/tr&gt;'<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #DD0000">'&lt;tr&gt;'</span><span style="color: #007700">;<br /><br />&nbsp;&nbsp;</span><span style="color: #FF9900">//&nbsp;Ersten&nbsp;Wochentag&nbsp;des&nbsp;Monats&nbsp;ermitteln&nbsp;und&nbsp;bis&nbsp;dahin&nbsp;Leerzellen&nbsp;hinzufügen<br />&nbsp;&nbsp;</span><span style="color: #0000BB">$first_day&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">date</span><span style="color: #007700">(</span><span style="color: #DD0000">'w'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$month_timestamp</span><span style="color: #007700">);<br />&nbsp;&nbsp;</span><span style="color: #0000BB">$fill_count&nbsp;</span><span style="color: #007700">=&nbsp;(</span><span style="color: #0000BB">$first_day&nbsp;</span><span style="color: #007700">==&nbsp;</span><span style="color: #0000BB">0&nbsp;</span><span style="color: #007700">?&nbsp;</span><span style="color: #0000BB">6&nbsp;</span><span style="color: #007700">:&nbsp;</span><span style="color: #0000BB">$first_day&nbsp;</span><span style="color: #007700">-&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">);<br />&nbsp;&nbsp;</span><span style="color: #0000BB">$output&nbsp;</span><span style="color: #007700">.=&nbsp;</span><span style="color: #0000BB">str_repeat</span><span style="color: #007700">(</span><span style="color: #DD0000">'&lt;td&gt;&amp;nbsp;&lt;/td&gt;'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$fill_count</span><span style="color: #007700">);<br /><br />&nbsp;&nbsp;</span><span style="color: #FF9900">//&nbsp;Monatstage&nbsp;auflisten<br />&nbsp;&nbsp;</span><span style="color: #0000BB">$day_count&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">date</span><span style="color: #007700">(</span><span style="color: #DD0000">'t'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$month_timestamp</span><span style="color: #007700">);<br />&nbsp;&nbsp;for&nbsp;(</span><span style="color: #0000BB">$i&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">;&nbsp;</span><span style="color: #0000BB">$i&nbsp;</span><span style="color: #007700">&lt;=&nbsp;</span><span style="color: #0000BB">$day_count</span><span style="color: #007700">;&nbsp;</span><span style="color: #0000BB">$i</span><span style="color: #007700">++)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF9900">//&nbsp;Tageszelle&nbsp;einfügen<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$output&nbsp;</span><span style="color: #007700">.=&nbsp;</span><span style="color: #DD0000">'&lt;td&gt;'&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$i&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #DD0000">'&lt;/td&gt;'</span><span style="color: #007700">;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF9900">//&nbsp;Neue&nbsp;Zeile&nbsp;nach&nbsp;7&nbsp;Spalten<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">if&nbsp;(++</span><span style="color: #0000BB">$fill_count&nbsp;</span><span style="color: #007700">%&nbsp;</span><span style="color: #0000BB">7&nbsp;</span><span style="color: #007700">==&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$output&nbsp;</span><span style="color: #007700">.=&nbsp;</span><span style="color: #DD0000">'&lt;/tr&gt;&lt;tr&gt;'</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;</span><span style="color: #FF9900">//&nbsp;Letzte&nbsp;Reihe&nbsp;mit&nbsp;Leerzellen&nbsp;auffüllen<br />&nbsp;&nbsp;</span><span style="color: #007700">while&nbsp;(</span><span style="color: #0000BB">$fill_count</span><span style="color: #007700">++&nbsp;%&nbsp;</span><span style="color: #0000BB">7&nbsp;</span><span style="color: #007700">!=&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$output&nbsp;</span><span style="color: #007700">.=&nbsp;</span><span style="color: #DD0000">'&lt;td&gt;&amp;nbsp;&lt;/td&gt;'</span><span style="color: #007700">;<br />&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;</span><span style="color: #FF9900">//&nbsp;Tabelle&nbsp;schließen<br />&nbsp;&nbsp;</span><span style="color: #0000BB">$output&nbsp;</span><span style="color: #007700">.=&nbsp;</span><span style="color: #DD0000">'&lt;/tr&gt;'<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #DD0000">'&lt;/table&gt;'</span><span style="color: #007700">;<br /><br />&nbsp;&nbsp;</span><span style="color: #FF9900">//&nbsp;Kalender&nbsp;zurückgeben<br />&nbsp;&nbsp;</span><span style="color: #007700">return&nbsp;</span><span style="color: #0000BB">$output</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
<p style="text-align: justify;">Aufgerufen werden kann die Funktion wie folgt:</p>
<div class="wpsp_code_div"><code><span style="color: #000000">
<span style="color: #007700">&lt;?</span><span style="color: #0000BB">php<br /><br /></span><span style="color: #FF9900">//&nbsp;Beispielaufruf&nbsp;1:&nbsp;Aktueller&nbsp;Monat<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">show_calendar</span><span style="color: #007700">();<br /><br /></span><span style="color: #FF9900">//&nbsp;Beispielaufruf&nbsp;2:&nbsp;Vorgegebener&nbsp;Monat<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">show_calendar</span><span style="color: #007700">(</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2009</span><span style="color: #007700">)<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
]]></content:encoded>
			<wfw:commentRss>http://andreas.droesch.de/2009/10/php-monatskalender-per-table/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP: HTML / XML Tag Attribute ermitteln</title>
		<link>http://andreas.droesch.de/2009/09/php-html-xml-tag-attribute-ermitteln/</link>
		<comments>http://andreas.droesch.de/2009/09/php-html-xml-tag-attribute-ermitteln/#comments</comments>
		<pubDate>Wed, 30 Sep 2009 19:43:40 +0000</pubDate>
		<dc:creator>Andreas</dc:creator>
				<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Beispiel]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[RegEx]]></category>
		<category><![CDATA[Strings]]></category>
		<category><![CDATA[Tag Attribute]]></category>

		<guid isPermaLink="false">http://andreas.droesch.de/?p=481</guid>
		<description><![CDATA[Dieser Codesnippet war ursprünglich ein Beispiel aus einem Forum, wo es darum ging die Attribute aller Img-Tags aus einem HTML Code auszulesen.
Ich habe den Code noch etwas erweitert und nun kann er allgemein genutzt werden, um die Attribute beliebiger Tags aus HTML oder XML Dokumenten zu ermitteln.

Die Funktion erwartet zwei Parameter. Als ersten den zu [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">Dieser Codesnippet war ursprünglich ein Beispiel aus einem Forum, wo es darum ging die Attribute aller Img-Tags aus einem HTML Code auszulesen.</p>
<p style="text-align: justify;">Ich habe den Code noch etwas erweitert und nun kann er allgemein genutzt werden, um die Attribute beliebiger Tags aus HTML oder XML Dokumenten zu ermitteln.</p>
<p><span id="more-481"></span></p>
<p style="text-align: justify;">Die Funktion erwartet zwei Parameter. Als ersten den zu durchsuchenden Code und als zweiten optional eine Einschränkung auf bestimmte Tagnamen:</p>
<div class="wpsp_code_div"><code><span style="color: #000000">
<span style="color: #007700">&lt;?</span><span style="color: #0000BB">php<br /><br /></span><span style="color: #FF9900">//&nbsp;HTML/XML&nbsp;Tag&nbsp;Attribute&nbsp;ermitteln<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">get_tag_attributes</span><span style="color: #007700">(</span><span style="color: #0000BB">$code</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$tag_search&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">false</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;</span><span style="color: #FF9900">//&nbsp;Einzelnen&nbsp;Tagnamen&nbsp;in&nbsp;ein&nbsp;Array&nbsp;packen<br />&nbsp;&nbsp;</span><span style="color: #007700">if&nbsp;(</span><span style="color: #0000BB">$tag_search&nbsp;</span><span style="color: #007700">&amp;&amp;&nbsp;!</span><span style="color: #0000BB">is_array</span><span style="color: #007700">(</span><span style="color: #0000BB">$tag_search</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$tag_search&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #0000BB">$tag_search</span><span style="color: #007700">);<br />&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;</span><span style="color: #FF9900">//&nbsp;Alle&nbsp;Tags&nbsp;auslesen<br />&nbsp;&nbsp;</span><span style="color: #0000BB">$matches&nbsp;</span><span style="color: #007700">=&nbsp;array();<br />&nbsp;&nbsp;</span><span style="color: #0000BB">preg_match_all</span><span style="color: #007700">(</span><span style="color: #DD0000">'/&lt;([a-z]*?)&nbsp;(.*?)\&gt;/is'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$code</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$matches</span><span style="color: #007700">);<br /><br />&nbsp;&nbsp;</span><span style="color: #FF9900">//&nbsp;Funde&nbsp;durchlaufen&nbsp;und&nbsp;Attribute&nbsp;ermitteln<br />&nbsp;&nbsp;</span><span style="color: #0000BB">$tags&nbsp;</span><span style="color: #007700">=&nbsp;array();<br />&nbsp;&nbsp;foreach&nbsp;(</span><span style="color: #0000BB">$matches</span><span style="color: #007700">[</span><span style="color: #0000BB">1</span><span style="color: #007700">]&nbsp;as&nbsp;</span><span style="color: #0000BB">$key&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$tag_name</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF9900">//&nbsp;Nicht&nbsp;zugelassene&nbsp;Tags&nbsp;überspringen<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">if&nbsp;(</span><span style="color: #0000BB">$tag_search</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(!</span><span style="color: #0000BB">in_array</span><span style="color: #007700">(</span><span style="color: #0000BB">strtolower</span><span style="color: #007700">(</span><span style="color: #0000BB">$tag_name</span><span style="color: #007700">),&nbsp;</span><span style="color: #0000BB">$tag_search</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;continue;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF9900">//&nbsp;Attribute&nbsp;austrennen<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$attributes&nbsp;</span><span style="color: #007700">=&nbsp;array();<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">preg_match_all</span><span style="color: #007700">(</span><span style="color: #DD0000">'/([a-z]*?)=(".*?"|\'.*?\')/is'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$matches</span><span style="color: #007700">[</span><span style="color: #0000BB">2</span><span style="color: #007700">][</span><span style="color: #0000BB">$key</span><span style="color: #007700">],&nbsp;</span><span style="color: #0000BB">$attributes</span><span style="color: #007700">);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF9900">//&nbsp;Attribute&nbsp;abspeichern<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$tag&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">'tag'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$tag_name</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;foreach&nbsp;(</span><span style="color: #0000BB">$attributes</span><span style="color: #007700">[</span><span style="color: #0000BB">1</span><span style="color: #007700">]&nbsp;as&nbsp;</span><span style="color: #0000BB">$key&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$value</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$tag</span><span style="color: #007700">[</span><span style="color: #0000BB">$value</span><span style="color: #007700">]&nbsp;=&nbsp;</span><span style="color: #0000BB">substr</span><span style="color: #007700">(</span><span style="color: #0000BB">$attributes</span><span style="color: #007700">[</span><span style="color: #0000BB">2</span><span style="color: #007700">][</span><span style="color: #0000BB">$key</span><span style="color: #007700">],&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;-</span><span style="color: #0000BB">1</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$tags</span><span style="color: #007700">[]&nbsp;=&nbsp;</span><span style="color: #0000BB">$tag</span><span style="color: #007700">;<br />&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;</span><span style="color: #FF9900">//&nbsp;Daten&nbsp;zurückgeben<br />&nbsp;&nbsp;</span><span style="color: #007700">return&nbsp;</span><span style="color: #0000BB">$tags</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
<p style="text-align: justify;">Ein Aufruf zur Suche nach Img-Tags in einem HTML Dokument könnte wie folgt aussehen:</p>
<div class="wpsp_code_div"><code><span style="color: #000000">
<span style="color: #007700">&lt;?</span><span style="color: #0000BB">php<br /><br /></span><span style="color: #FF9900">//&nbsp;HTML&nbsp;Code&nbsp;für&nbsp;die&nbsp;Beispielaufrufe<br /></span><span style="color: #0000BB">$html_code&nbsp;</span><span style="color: #007700">=&nbsp;&lt;&lt;&lt;&nbsp;CODE_BLOCK<br /></span><span style="color: #0000BB">&lt;html&gt;<br />&lt;head&gt;<br />&nbsp;&nbsp;&lt;title&gt;Test&lt;/title&gt;<br />&lt;/head&gt;<br />&lt;body&gt;<br /><br />&nbsp;&nbsp;&lt;img&nbsp;src="bild1.jpg"&nbsp;alt="img1"&nbsp;style="border:1px&nbsp;solid&nbsp;red"&nbsp;/&gt;<br /><br />&nbsp;&nbsp;&lt;img&nbsp;src="bild2.jpg"&nbsp;alt="img2"&nbsp;style="border:1px&nbsp;solid&nbsp;green"&nbsp;/&gt;<br /><br />&nbsp;&nbsp;&lt;div&nbsp;style="background-color:red"&gt;<br />&nbsp;&nbsp;Test<br />&nbsp;&nbsp;&lt;/div&gt;<br /><br />&nbsp;&nbsp;&lt;p&nbsp;style="color:red"&gt;Test&lt;/p&gt;<br /><br />&lt;/body&gt;<br />&lt;/html&gt;<br /></span><span style="color: #007700">CODE_BLOCK;<br /><br /></span><span style="color: #FF9900">//&nbsp;Beispielaufruf&nbsp;1:&nbsp;Nur&nbsp;Img-Tags&nbsp;durchsuchen<br /></span><span style="color: #0000BB">$tags&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">get_tag_attributes</span><span style="color: #007700">(</span><span style="color: #0000BB">$html_code</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'img'</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF9900">//&nbsp;Array&nbsp;formatiert&nbsp;ausgeben<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #DD0000">'&lt;pre&gt;'</span><span style="color: #007700">;&nbsp;</span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$tags</span><span style="color: #007700">);&nbsp;echo&nbsp;</span><span style="color: #DD0000">'&lt;/pre&gt;'</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
<p style="text-align: justify;">Hier wurde als zweiter Parameter ein einzelner Tagname in einem String übergeben. Das zurückgegebene Array hat folgenden Aufbau:</p>
<div class="wpsp_code_div"><code>Array<br />
(<br />
&nbsp;&nbsp;[0] =&gt; Array<br />
&nbsp;&nbsp;&nbsp;&nbsp;(<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[tag] =&gt; img<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[src] =&gt; bild1.jpg<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[alt] =&gt; img1<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[style] =&gt; border:1px solid red<br />
&nbsp;&nbsp;&nbsp;&nbsp;)<br />
<br />
&nbsp;&nbsp;[1] =&gt; Array<br />
&nbsp;&nbsp;&nbsp;&nbsp;(<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[tag] =&gt; img<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[src] =&gt; bild2.jpg<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[alt] =&gt; img2<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[style] =&gt; border:1px solid green<br />
&nbsp;&nbsp;&nbsp;&nbsp;)<br />
)</code></div>
<p style="text-align: justify;">Alternativ kann als zweiter Parameter auch ein Array mit mehreren Tagnamen übergeben werden:</p>
<div class="wpsp_code_div"><code><span style="color: #000000">
<span style="color: #007700">&lt;?</span><span style="color: #0000BB">php<br /><br /></span><span style="color: #FF9900">//&nbsp;Beispielaufruf&nbsp;2:&nbsp;Nur&nbsp;Div-&nbsp;und&nbsp;P-Tags&nbsp;durchsuchen<br /></span><span style="color: #0000BB">$tags&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">get_tag_attributes</span><span style="color: #007700">(</span><span style="color: #0000BB">$html_code</span><span style="color: #007700">,&nbsp;array(</span><span style="color: #DD0000">'div'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'p'</span><span style="color: #007700">));<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
<p style="text-align: justify;">Sollen alle Tags durchsucht werden, so wird der zweite Parameter einfach komplett weggelassen:</p>
<div class="wpsp_code_div"><code><span style="color: #000000">
<span style="color: #007700">&lt;?</span><span style="color: #0000BB">php<br /><br /></span><span style="color: #FF9900">//&nbsp;Beispielaufruf&nbsp;3:&nbsp;Alle&nbsp;Tags&nbsp;durchsuchen<br /></span><span style="color: #0000BB">$tags&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">get_tag_attributes</span><span style="color: #007700">(</span><span style="color: #0000BB">$html_code</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
]]></content:encoded>
			<wfw:commentRss>http://andreas.droesch.de/2009/09/php-html-xml-tag-attribute-ermitteln/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP: Datei- und Bildtyp ermitteln</title>
		<link>http://andreas.droesch.de/2009/09/php-datei-und-bildtyp-ermitteln/</link>
		<comments>http://andreas.droesch.de/2009/09/php-datei-und-bildtyp-ermitteln/#comments</comments>
		<pubDate>Mon, 21 Sep 2009 18:37:55 +0000</pubDate>
		<dc:creator>Andreas</dc:creator>
				<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Bildtyp]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Dateityp]]></category>
		<category><![CDATA[MIME-Typ]]></category>

		<guid isPermaLink="false">http://andreas.droesch.de/?p=379</guid>
		<description><![CDATA[Wer beispielsweise mit Uploadformularen arbeitet oder Bilder durch eine PHP Datei weiterleitet kommt früher oder später an einen Punk, an welchem der entsprechende Dateityp der Datei ermittelt werden soll. Je nach Anwendungszweck gibt es  verschiedenen Möglichkeiten die Dateierweiterung oder den MIME-Typ zu ermitteln:

MIME-Typ im $_FILES Array
Direkt nach dem Upload einer Datei stehen im $_FILES [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">Wer beispielsweise mit Uploadformularen arbeitet oder Bilder durch eine PHP Datei weiterleitet kommt früher oder später an einen Punk, an welchem der entsprechende Dateityp der Datei ermittelt werden soll. Je nach Anwendungszweck gibt es  verschiedenen Möglichkeiten die Dateierweiterung oder den <a href="http://de.wikipedia.org/wiki/Multipurpose_Internet_Mail_Extensions" target="_blank">MIME-Typ</a> zu ermitteln:</p>
<p><span id="more-379"></span></p>
<h4>MIME-Typ im $_FILES Array</h4>
<p style="text-align: justify;">Direkt nach dem Upload einer Datei stehen im <strong><a href="http://php.net/manual/de/features.file-upload.post-method.php" target="_blank">$_FILES</a></strong> Array folgende Angaben zur Verfügung:</p>
<ul style="margin-bottom: 20px;">
<li><span style="color: #808080;">name: Originaler Dateiname der Datei</span></li>
<li><strong>type</strong>: MIME-Typ der Datei</li>
<li><span style="color: #808080;">size: Größe in Bytes</span></li>
<li><span style="color: #808080;">tmp_name: Name unter welchem die Datei auf dem Server zwischengespeichert wurde</span></li>
<li><span style="color: #808080;">error: Statuscode des Uploadvorgangs</span></li>
</ul>
<p style="text-align: justify;">In <strong>type</strong> finden wir also den gesuchten MIME-Typ der Datei und können diesen weiterverarbeiten:</p>
<div class="wpsp_code_div"><code><span style="color: #000000">
<span style="color: #007700">&lt;?</span><span style="color: #0000BB">php<br /><br /></span><span style="color: #007700">if&nbsp;(</span><span style="color: #0000BB">$_FILES</span><span style="color: #007700">[</span><span style="color: #DD0000">'myUpload'</span><span style="color: #007700">][</span><span style="color: #DD0000">'type'</span><span style="color: #007700">]&nbsp;==&nbsp;</span><span style="color: #DD0000">'image/png'</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;</span><span style="color: #FF9900">//&nbsp;Nur&nbsp;PNG-Grafiken&nbsp;verarbeiten<br />&nbsp;&nbsp;//&nbsp;...<br /></span><span style="color: #007700">}<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
<h4>MIME-Typ durch getimagesize()</h4>
<p style="text-align: justify;">Um den MIME-Typ bei Bildern auch im nachhinein noch zu ermitteln kann man die Funktion <strong><a href="http://php.net/getimagesize" target="_blank">getimagesize()</a></strong> benutzen, welche folgende Daten zurück liefert:</p>
<ul style="margin-bottom: 20px;">
<li><span style="color: #808080;">0: Breite des Bildes</span></li>
<li><span style="color: #808080;">1: Höhe des Bildes</span></li>
<li><span style="color: #808080;">2: PHP spezifische Bildtyp Konstante</span></li>
<li><span style="color: #808080;">3: Maßangaben als HTML Attribut</span></li>
<li><strong>mime</strong>: MIME-Typ der Datei</li>
</ul>
<p style="text-align: justify;">Hier finden wir unter <strong>mime</strong> den gesuchten Typen:</p>
<div class="wpsp_code_div"><code><span style="color: #000000">
<span style="color: #007700">&lt;?</span><span style="color: #0000BB">php<br /><br />$file&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'bild.png'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$image_data&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">getimagesize</span><span style="color: #007700">(</span><span style="color: #0000BB">$file</span><span style="color: #007700">);<br /><br />if&nbsp;(</span><span style="color: #0000BB">$image_data</span><span style="color: #007700">[</span><span style="color: #DD0000">'mime'</span><span style="color: #007700">]&nbsp;==&nbsp;</span><span style="color: #DD0000">'image/png'</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;</span><span style="color: #FF9900">//&nbsp;Nur&nbsp;PNG-Grafiken&nbsp;verarbeiten<br />&nbsp;&nbsp;//&nbsp;...<br /></span><span style="color: #007700">}<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
<p style="text-align: justify;">Kleiner Hinweis zu den gängigsten Bildtypen, neben <strong>image/png</strong> und <strong>image/gif</strong> muss für JPG Bilder immer <strong>image/jpeg</strong>, also die lange Form genutzt werden. Wer auf der sicheren Seite sein will fügt zusätzlich noch <strong>image/pjpeg</strong> seiner Abfrage hinzu. Das <strong>p</strong> steht für progressiv und gerade der IE sendet diesen Typ bei JPGs gerne mit. Hier noch eine kleine Kontrollabfrage als Beispiel:</p>
<div class="wpsp_code_div"><code><span style="color: #000000">
<span style="color: #007700">&lt;?</span><span style="color: #0000BB">php<br /><br /></span><span style="color: #FF9900">//&nbsp;Erlaubte&nbsp;Bildtypen<br /></span><span style="color: #0000BB">$mime_list&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">'image/jpeg'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'image/pjpeg'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'image/png'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'image/gif'</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF9900">//&nbsp;Dateiupload&nbsp;auf&nbsp;gültigen&nbsp;Typ&nbsp;prüfen<br /></span><span style="color: #007700">if&nbsp;(!</span><span style="color: #0000BB">in_array</span><span style="color: #007700">(</span><span style="color: #0000BB">$_FILES</span><span style="color: #007700">[</span><span style="color: #DD0000">'myupload'</span><span style="color: #007700">][</span><span style="color: #DD0000">'type'</span><span style="color: #007700">],&nbsp;</span><span style="color: #0000BB">$mime_list</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">'Es&nbsp;sind&nbsp;nur&nbsp;Dateien&nbsp;vom&nbsp;Typ&nbsp;JPG,&nbsp;PNG&nbsp;und&nbsp;GIF&nbsp;erlaubt.'</span><span style="color: #007700">;<br />}&nbsp;else&nbsp;{<br />&nbsp;&nbsp;</span><span style="color: #FF9900">//&nbsp;Daten&nbsp;weiterverarbeiten<br />&nbsp;&nbsp;//&nbsp;...<br /></span><span style="color: #007700">}<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
<h4>Erweiterung im Dateinamen</h4>
<p style="text-align: justify;">Soll dagegen die Dateierweiterung direkt ermittelt werden, so muss man den Dateinamen zerlegen:</p>
<div class="wpsp_code_div"><code><span style="color: #000000">
<span style="color: #007700">&lt;?</span><span style="color: #0000BB">php<br /><br /></span><span style="color: #FF9900">//&nbsp;Dateierweiterung&nbsp;auslesen<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">get_file_ext</span><span style="color: #007700">(</span><span style="color: #0000BB">$filename</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">strtolower</span><span style="color: #007700">(</span><span style="color: #0000BB">substr</span><span style="color: #007700">(</span><span style="color: #0000BB">strrchr</span><span style="color: #007700">(</span><span style="color: #0000BB">$filename</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'.'</span><span style="color: #007700">),&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">));<br />}<br /><br /></span><span style="color: #FF9900">//&nbsp;Beispielaufruf<br /></span><span style="color: #0000BB">$file&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'download.zip'</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #0000BB">get_file_ext</span><span style="color: #007700">(</span><span style="color: #0000BB">$file</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF9900">//&nbsp;=&nbsp;zip<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
<p style="text-align: justify;">Die Funktion liefert immer die Erweiterung hinter dem letzten Punk in Kleinbuchstaben.</p>
]]></content:encoded>
			<wfw:commentRss>http://andreas.droesch.de/2009/09/php-datei-und-bildtyp-ermitteln/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>PHP: if-else mit dem Trinitäts-Operator</title>
		<link>http://andreas.droesch.de/2009/09/php-if-else-mit-dem-trinitats-operator/</link>
		<comments>http://andreas.droesch.de/2009/09/php-if-else-mit-dem-trinitats-operator/#comments</comments>
		<pubDate>Mon, 07 Sep 2009 20:14:46 +0000</pubDate>
		<dc:creator>Andreas</dc:creator>
				<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[if-else]]></category>

		<guid isPermaLink="false">http://andreas.droesch.de/?p=307</guid>
		<description><![CDATA[Die ganz normale if-else -Bedingung ist sicher jedem geläufig, doch nicht jeder kennt den Trinitäts-Operator, welcher die Bedingung in manchen Fällen deutlich verkürzen kann.

Nachfolgend ein erstes Beispiel, bei welchem die aktuelle Uhrzeit ermittelt und eine passende Anrede ausgewählt wird:

&#60;?php//&#160;Uhrzeit&#160;ermitteln&#160;(Stunden&#160;ohne&#160;führende&#160;Null)$time&#160;=&#160;date('G');//&#160;Anrede&#160;ausgebenif&#160;($time&#160;&#60;&#160;18)&#160;{&#160;&#160;echo&#160;'Guten&#160;Tag';}&#160;else&#160;{&#160;&#160;echo&#160;'Guten&#160;Abend';}?&#62;


Nun kommt der  Trinitäts-Operator &#8220;?&#8221; ins Spiel, dieser ist wie folgt aufgebaut:
(Bedingung ? wenn true : [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">Die ganz normale<em> if-else -</em>Bedingung ist sicher jedem geläufig, doch nicht jeder kennt den <em>Trinitäts</em>-Operator, welcher die Bedingung in manchen Fällen deutlich verkürzen kann.</p>
<p><span id="more-307"></span></p>
<p style="text-align: justify;">Nachfolgend ein erstes Beispiel, bei welchem die aktuelle Uhrzeit ermittelt und eine passende Anrede ausgewählt wird:</p>
<div class="wpsp_code_div"><code><span style="color: #000000">
<span style="color: #007700">&lt;?</span><span style="color: #0000BB">php<br /><br /></span><span style="color: #FF9900">//&nbsp;Uhrzeit&nbsp;ermitteln&nbsp;(Stunden&nbsp;ohne&nbsp;führende&nbsp;Null)<br /></span><span style="color: #0000BB">$time&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">date</span><span style="color: #007700">(</span><span style="color: #DD0000">'G'</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF9900">//&nbsp;Anrede&nbsp;ausgeben<br /></span><span style="color: #007700">if&nbsp;(</span><span style="color: #0000BB">$time&nbsp;</span><span style="color: #007700">&lt;&nbsp;</span><span style="color: #0000BB">18</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">'Guten&nbsp;Tag'</span><span style="color: #007700">;<br />}&nbsp;else&nbsp;{<br />&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">'Guten&nbsp;Abend'</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
<p style="text-align: justify;">Nun kommt der  <em>Trinitäts</em>-Operator &#8220;<strong>?</strong>&#8221; ins Spiel, dieser ist wie folgt aufgebaut:</p>
<div class="wpsp_code_div"><code>(Bedingung ? wenn true : wenn false)</code></div>
<p style="text-align: justify;">Auf das erste Beispiel bezogen würde dies nun so aussehen:</p>
<div class="wpsp_code_div"><code><span style="color: #000000">
<span style="color: #007700">&lt;?</span><span style="color: #0000BB">php<br /><br /></span><span style="color: #FF9900">//&nbsp;Uhrzeit&nbsp;ermitteln&nbsp;(Stunden&nbsp;ohne&nbsp;führende&nbsp;Null)<br /></span><span style="color: #0000BB">$time&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">date</span><span style="color: #007700">(</span><span style="color: #DD0000">'G'</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF9900">//&nbsp;Anrede&nbsp;ausgeben<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #DD0000">'Guten&nbsp;'&nbsp;</span><span style="color: #007700">.&nbsp;(</span><span style="color: #0000BB">$time&nbsp;</span><span style="color: #007700">&lt;&nbsp;</span><span style="color: #0000BB">18&nbsp;</span><span style="color: #007700">?&nbsp;</span><span style="color: #DD0000">'Tag'&nbsp;</span><span style="color: #007700">:&nbsp;</span><span style="color: #DD0000">'Abend'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
<p style="text-align: justify;">Zuerst kommt die Bedingung mit <strong>$time &lt; 18</strong>, ist diese wahr (true), so wird der Code vor dem Doppelpunkt verarbeitet, andernfalls der hinter dem Doppelpunkt.</p>
<p style="text-align: justify;">Hier noch ein zweites Beispiel zur Verdeutlichung. Eine kleine Funktion, welche die größere von zwei übergebenen Zahlen ermittelt (ähnlich der Standardfunktion <a href="http://php.net/max" target="_blank">max()</a>). Zuerst die normale Schreibweise:</p>
<div class="wpsp_code_div"><code><span style="color: #000000">
<span style="color: #007700">&lt;?</span><span style="color: #0000BB">php<br /><br /></span><span style="color: #FF9900">//&nbsp;Eigene&nbsp;Max-Funktion<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">my_max</span><span style="color: #007700">(</span><span style="color: #0000BB">$a</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$b</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;if&nbsp;(</span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">&gt;&nbsp;</span><span style="color: #0000BB">$b</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">$a</span><span style="color: #007700">;<br />&nbsp;&nbsp;}&nbsp;else&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">$b</span><span style="color: #007700">;<br />&nbsp;&nbsp;}<br />}<br /><br />echo&nbsp;</span><span style="color: #0000BB">my_max</span><span style="color: #007700">(</span><span style="color: #0000BB">2</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">5</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF9900">//&nbsp;=&nbsp;5<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
<p style="text-align: justify;">Und hier die kürzere Fassung:</p>
<div class="wpsp_code_div"><code><span style="color: #000000">
<span style="color: #007700">&lt;?</span><span style="color: #0000BB">php<br /><br /></span><span style="color: #FF9900">//&nbsp;Eigene&nbsp;Max-Funktion<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">my_max</span><span style="color: #007700">(</span><span style="color: #0000BB">$a</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$b</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;return&nbsp;(</span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">&gt;&nbsp;</span><span style="color: #0000BB">$b&nbsp;</span><span style="color: #007700">?&nbsp;</span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">:&nbsp;</span><span style="color: #0000BB">$b</span><span style="color: #007700">);<br />}<br /><br />echo&nbsp;</span><span style="color: #0000BB">my_max</span><span style="color: #007700">(</span><span style="color: #0000BB">2</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">5</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF9900">//&nbsp;=&nbsp;5<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
]]></content:encoded>
			<wfw:commentRss>http://andreas.droesch.de/2009/09/php-if-else-mit-dem-trinitats-operator/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>PHP: Tageszahl zwischen zwei Daten</title>
		<link>http://andreas.droesch.de/2009/09/php-tageszahl-zwischen-zwei-daten/</link>
		<comments>http://andreas.droesch.de/2009/09/php-tageszahl-zwischen-zwei-daten/#comments</comments>
		<pubDate>Sat, 05 Sep 2009 15:19:02 +0000</pubDate>
		<dc:creator>Andreas</dc:creator>
				<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Datum]]></category>
		<category><![CDATA[Tageszahl]]></category>

		<guid isPermaLink="false">http://andreas.droesch.de/?p=289</guid>
		<description><![CDATA[Bei der Ermittlung der Tageszahl zwischen zwei Daten kann einem die kleinen Funktion gregoriantojd() sehr hilfreich sein. Diese wandelt ein Datum aus dem Gregorianischen Kalender (so wie wir ihn meist kennen) in ein Julianisches Datum um, welches ähnlich dem Timestamp ein Datum als Ganzzahl wiedergibt, jedoch nicht als Sekunden, sondern als Tage.

Die folgende Funktion erwartet [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">Bei der Ermittlung der Tageszahl zwischen zwei Daten kann einem die kleinen Funktion <strong><a href="http://php.net/gregoriantojd" target="_blank">gregoriantojd()</a></strong> sehr hilfreich sein. Diese wandelt ein Datum aus dem <a href="http://de.wikipedia.org/wiki/Gregorianischer_Kalender" target="_blank">Gregorianischen Kalender</a> (so wie wir ihn meist kennen) in ein <a href="http://de.wikipedia.org/wiki/Julianisches_Datum" target="_blank">Julianisches Datum</a> um, welches ähnlich dem Timestamp ein Datum als Ganzzahl wiedergibt, jedoch nicht als Sekunden, sondern als Tage.</p>
<p><span id="more-289"></span></p>
<p style="text-align: justify;">Die folgende Funktion erwartet zwei Daten als Parameter, wahlweise als Timestamp oder Datumsstring:</p>
<div class="wpsp_code_div"><code><span style="color: #000000">
<span style="color: #007700">&lt;?</span><span style="color: #0000BB">php<br /><br /></span><span style="color: #FF9900">//&nbsp;Tageszahl&nbsp;zwischen&nbsp;zwei&nbsp;Daten&nbsp;ermitteln<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">get_days_between</span><span style="color: #007700">(</span><span style="color: #0000BB">$date1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$date2</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;</span><span style="color: #FF9900">//&nbsp;Timestamp&nbsp;erzeugen,&nbsp;falls&nbsp;nicht&nbsp;gegeben<br />&nbsp;&nbsp;</span><span style="color: #007700">if&nbsp;(!</span><span style="color: #0000BB">is_numeric</span><span style="color: #007700">(</span><span style="color: #0000BB">$date1</span><span style="color: #007700">))&nbsp;{&nbsp;</span><span style="color: #0000BB">$date1&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">strtotime</span><span style="color: #007700">(</span><span style="color: #0000BB">$date1</span><span style="color: #007700">);&nbsp;}<br />&nbsp;&nbsp;if&nbsp;(!</span><span style="color: #0000BB">is_numeric</span><span style="color: #007700">(</span><span style="color: #0000BB">$date2</span><span style="color: #007700">))&nbsp;{&nbsp;</span><span style="color: #0000BB">$date2&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">strtotime</span><span style="color: #007700">(</span><span style="color: #0000BB">$date2</span><span style="color: #007700">);&nbsp;}<br /><br />&nbsp;&nbsp;</span><span style="color: #FF9900">//&nbsp;Daten&nbsp;in&nbsp;Julianisches&nbsp;Datum&nbsp;umwandeln<br />&nbsp;&nbsp;</span><span style="color: #0000BB">$date1&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">gregoriantojd</span><span style="color: #007700">(</span><span style="color: #0000BB">date</span><span style="color: #007700">(</span><span style="color: #DD0000">'m'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$date1</span><span style="color: #007700">),&nbsp;</span><span style="color: #0000BB">date</span><span style="color: #007700">(</span><span style="color: #DD0000">'j'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$date1</span><span style="color: #007700">),&nbsp;</span><span style="color: #0000BB">date</span><span style="color: #007700">(</span><span style="color: #DD0000">'Y'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$date1</span><span style="color: #007700">));<br />&nbsp;&nbsp;</span><span style="color: #0000BB">$date2&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">gregoriantojd</span><span style="color: #007700">(</span><span style="color: #0000BB">date</span><span style="color: #007700">(</span><span style="color: #DD0000">'m'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$date2</span><span style="color: #007700">),&nbsp;</span><span style="color: #0000BB">date</span><span style="color: #007700">(</span><span style="color: #DD0000">'j'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$date2</span><span style="color: #007700">),&nbsp;</span><span style="color: #0000BB">date</span><span style="color: #007700">(</span><span style="color: #DD0000">'Y'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$date2</span><span style="color: #007700">));<br /><br />&nbsp;&nbsp;</span><span style="color: #FF9900">//&nbsp;Differenz&nbsp;ermitteln&nbsp;und&nbsp;zurückgeben<br />&nbsp;&nbsp;</span><span style="color: #007700">return&nbsp;(</span><span style="color: #0000BB">$date1&nbsp;</span><span style="color: #007700">&gt;&nbsp;</span><span style="color: #0000BB">$date2&nbsp;</span><span style="color: #007700">?&nbsp;</span><span style="color: #0000BB">$date1&nbsp;</span><span style="color: #007700">-&nbsp;</span><span style="color: #0000BB">$date2&nbsp;</span><span style="color: #007700">:&nbsp;</span><span style="color: #0000BB">$date2&nbsp;</span><span style="color: #007700">-&nbsp;</span><span style="color: #0000BB">$date1</span><span style="color: #007700">);<br />}<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
<p>Ein Aufruf mit Timestamp könnte wie folgt aussehen:</p>
<div class="wpsp_code_div"><code><span style="color: #000000">
<span style="color: #007700">&lt;?</span><span style="color: #0000BB">php<br /><br /></span><span style="color: #FF9900">//&nbsp;Beispielaufruf&nbsp;1:&nbsp;Timestamp<br /></span><span style="color: #0000BB">$time1&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">1251756000</span><span style="color: #007700">;</span><span style="color: #0000BB"> </span><span style="color: #FF9900">//&nbsp;01.09.2009<br /></span><span style="color: #0000BB">$time2&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">1253829600</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF9900">//&nbsp;25.09.2009<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #DD0000">'Tagesdifferenz:&nbsp;'&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">get_days_between</span><span style="color: #007700">(</span><span style="color: #0000BB">$time1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$time2</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF9900">//&nbsp;=&nbsp;24<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
<p>Und mit einem normalen Datum in Textform:</p>
<div class="wpsp_code_div"><code><span style="color: #000000">
<span style="color: #007700">&lt;?</span><span style="color: #0000BB">php<br /><br /></span><span style="color: #FF9900">//&nbsp;Beispielaufruf&nbsp;2:&nbsp;Datumsstring<br /></span><span style="color: #0000BB">$time1&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'01.09.2009'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$time2&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'25.09.2009'</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #DD0000">'Tagesdifferenz:&nbsp;'&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">get_days_between</span><span style="color: #007700">(</span><span style="color: #0000BB">$time1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$time2</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF9900">//&nbsp;=&nbsp;24<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
]]></content:encoded>
			<wfw:commentRss>http://andreas.droesch.de/2009/09/php-tageszahl-zwischen-zwei-daten/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

