<?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>naden.de/blog &#187; Php</title> <atom:link href="http://www.naden.de/blog/category/php/feed" rel="self" type="application/rss+xml" /><link>http://www.naden.de/blog</link> <description>/* no comment */</description> <lastBuildDate>Fri, 30 Jul 2010 09:41:23 +0000</lastBuildDate> <generator>http://wordpress.org/?v=2.9.1</generator> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <item><title>Curl PHP Get Request</title><link>http://www.naden.de/blog/curl-php-get-request</link> <comments>http://www.naden.de/blog/curl-php-get-request#comments</comments> <pubDate>Wed, 20 Jan 2010 15:55:12 +0000</pubDate> <dc:creator>naden</dc:creator> <category><![CDATA[Php]]></category> <category><![CDATA[conten type]]></category> <category><![CDATA[curl]]></category> <category><![CDATA[download]]></category> <category><![CDATA[get]]></category> <category><![CDATA[get request]]></category> <category><![CDATA[http]]></category> <category><![CDATA[http get]]></category><guid
isPermaLink="false">http://www.naden.de/blog/curl-php-get-request</guid> <description><![CDATA[M&#246;chte man mit curl z.B. Dateien mit dem Content-Type: text/html runter laden, kann sich aber wegen einer fehlenden Endung (html, css, &#8230;) nicht an dieser orientieren, so springt die folgende Funktion in die Bresche./**
* @param string url - Url
* @param array content_types - Liste der erlaubten Content-Typen
* @param string file - (optional) [...]]]></description> <content:encoded><![CDATA[<p>M&#246;chte man mit <em>curl</em> z.B. Dateien mit dem <em>Content-Type: text/html</em> runter laden, kann sich aber wegen einer fehlenden Endung (html, css, &#8230;) nicht an dieser orientieren, so springt die folgende Funktion in die Bresche.<span
id="more-150"></span></p><div
class="wp_codebox"><table><tr
id="p1503"><td
class="code" id="p150code3"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
 * @param string url - Url
 * @param array content_types - Liste der erlaubten Content-Typen
 * @param string file - (optional) Datei um die empfangenen Daten zu speichern    
 * 
 * @return mixed true | false | Daten 
 */</span> 
<span style="color: #000000; font-weight: bold;">function</span> curlGetByContentType<span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #339933;">,</span> <span style="color: #000088;">$content_types</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'text/html'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$file</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// curl mit Url initialisieren</span>
  <span style="color: #000088;">$ch</span> <span style="color: #339933;">=</span> <span style="color: #990000;">curl_init</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// optionen setzen: nur header zurückliefern</span>
  <span style="color: #990000;">curl_setopt_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
    CURLOPT_HEADER <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">,</span>
    CURLOPT_NOBODY <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">,</span>
    CURLOPT_RETURNTRANSFER <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">true</span>
  <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #990000;">curl_exec</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// prüfe ob der Content-Type einer der geforderten ist</span>
  <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">eregi</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'^('</span><span style="color: #339933;">.</span> <span style="color: #990000;">implode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'|'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$content_types</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span> <span style="color: #0000ff;">')'</span><span style="color: #339933;">,</span> <span style="color: #990000;">curl_getinfo</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLINFO_CONTENT_TYPE<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// curl Handle schliessen</span>
    <span style="color: #990000;">curl_close</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// curl mit Url initialisieren </span>
    <span style="color: #000088;">$ch</span> <span style="color: #339933;">=</span> <span style="color: #990000;">curl_init</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// optionen: Body-Cotnent zurückliefern</span>
    <span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_RETURNTRANSFER<span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// wenn eine Datei angeg. wurde, Daten in Datei speichern</span>
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">is_null</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$fh</span> <span style="color: #339933;">=</span> <span style="color: #990000;">fopen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'w'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_FILE<span style="color: #339933;">,</span> <span style="color: #000088;">$fh</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #990000;">curl_exec</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// curl Hadle schliessen</span>
  <span style="color: #990000;">curl_close</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #666666; font-style: italic;">// Datei Handle schliessen</span>
  <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fh</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$fh</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #990000;">fclose</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fh</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #b1b100;">return</span> <span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span> ? <span style="color: #000088;">$data</span> <span style="color: #339933;">:</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div><p><strong>Beispiele</strong></p><div
class="wp_codebox"><table><tr
id="p1504"><td
class="code" id="p150code4"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># Content-Type: text/html
</span><span style="color: #000088;">$url</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'http://www.golem.de'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> curlGetByContentType<span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'image/png'</span><span style="color: #339933;">,</span> <span style="color: #339933;">,</span> <span style="color: #0000ff;">'text/html'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Content-Type: text/html; charset=&quot;UTF-8&quot;
</span><span style="color: #000088;">$url</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'http://www.heise.de'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> curlGetByContentType<span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'image/png'</span><span style="color: #339933;">,</span> <span style="color: #339933;">,</span> <span style="color: #0000ff;">'text/html'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Content-Type: image/png
</span><span style="color: #000088;">$url</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'http://upload.wikimedia.org/wikipedia/de/b/bc/Wiki.png'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> curlGetByContentType<span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'image/png'</span><span style="color: #339933;">,</span> <span style="color: #339933;">,</span> <span style="color: #0000ff;">'text/html'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'file.png'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div><p>Leider habe ich keinen Weg gefunden, ohne das <em>curl</em> zwei mal initialisiert werden muss. Vorschl&#228;ge sind willkommen.</p> ]]></content:encoded> <wfw:commentRss>http://www.naden.de/blog/curl-php-get-request/feed</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>PHP Redirect</title><link>http://www.naden.de/blog/php-redirect</link> <comments>http://www.naden.de/blog/php-redirect#comments</comments> <pubDate>Fri, 15 Jan 2010 17:58:51 +0000</pubDate> <dc:creator>naden</dc:creator> <category><![CDATA[Php]]></category> <category><![CDATA[idn]]></category> <category><![CDATA[punnycode]]></category> <category><![CDATA[redirect]]></category> <category><![CDATA[umlautdomains]]></category> <category><![CDATA[umlaute]]></category><guid
isPermaLink="false">http://www.naden.de/blog/php-redirect</guid> <description><![CDATA[PHP Redirects von Domains sind ein alter Hut. W&#228;hrend man Domains ohne Sonderzeichen problemlos mittels header(&#8216;Location: &#8230;&#8217;); oder Meta-Refresh umleiten kann, hat man bei Sonderzeichen in Domainnamen ein Problem.
Der Meta-Refresh funktioniert zwar noch immer, der Redirect mittels header(&#8216;Location: &#8230;&#8217;); aus PHP heraus leider nicht.
Die folgende Funktion zerlegt die Url&#8217;s in ihre Bestandteile, dann wird der [...]]]></description> <content:encoded><![CDATA[<p>PHP Redirects von Domains sind ein alter Hut. W&#228;hrend man Domains ohne Sonderzeichen problemlos mittels <em>header(&#8216;Location: &#8230;&#8217;);</em> oder <em>Meta-Refresh</em> umleiten kann, hat man bei Sonderzeichen in Domainnamen ein Problem.<span
id="more-148"></span></p><p>Der <em>Meta-Refresh</em> funktioniert zwar noch immer, der Redirect mittels <em>header(&#8216;Location: &#8230;&#8217;);</em> aus PHP heraus leider nicht.</p><p>Die folgende Funktion zerlegt die Url&#8217;s in ihre Bestandteile, dann wird der Hostname in Punnycode codiert und die Url wieder zusammengbaut.</p><p>Der Aufruf von <em>idn_redirect(&#8216;http://www.&#246;ko.de/&#8217;); leitet auf </em><em>http://www.xn--ko-eka.de/</em></p><div
class="wp_codebox"><table><tr
id="p1486"><td
class="code" id="p148code6"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> idn_redirect<span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #339933;">,</span> <span style="color: #000088;">$permanent</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #b1b100;">include_once</span> <span style="color: #0000ff;">'Net/IDNA.php'</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000088;">$idn</span> <span style="color: #339933;">=</span> Net_IDNA<span style="color: #339933;">::</span><span style="color: #004000;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000088;">$tokens</span> <span style="color: #339933;">=</span> <span style="color: #990000;">parse_url</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000088;">$target</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$tokens</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'scheme'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'://'</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array_key_exists</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'user'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$tokens</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #990000;">array_key_exists</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'pass'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$tokens</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$target</span> <span style="color: #339933;">.=</span> <span style="color: #000088;">$tokens</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'user'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span> <span style="color: #0000ff;">':'</span><span style="color: #339933;">.</span> <span style="color: #000088;">$tokens</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'pass'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span> <span style="color: #0000ff;">'@'</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  try <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$host</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$idn</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">encode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$tokens</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'host'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  catch<span style="color: #009900;">&#40;</span>Exception <span style="color: #000088;">$ex</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$host</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$target</span> <span style="color: #339933;">.=</span> <span style="color: #000088;">$host</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$target</span> <span style="color: #339933;">.=</span> <span style="color: #000088;">$tokens</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'host'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array_key_exists</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'port'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$tokens</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #339933;">!</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$tokens</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'port'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$target</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">':'</span><span style="color: #339933;">.</span> <span style="color: #000088;">$tokens</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'port'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array_key_exists</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'path'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$tokens</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #339933;">!</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$tokens</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'path'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$target</span> <span style="color: #339933;">.=</span> <span style="color: #000088;">$tokens</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'path'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array_key_exists</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'query'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$tokens</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #339933;">!</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$tokens</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'query'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$target</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'?'</span><span style="color: #339933;">.</span> <span style="color: #000088;">$tokens</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'query'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array_key_exists</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'fragment'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$tokens</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #339933;">!</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$tokens</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'fragment'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$target</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'#'</span><span style="color: #339933;">.</span> <span style="color: #000088;">$tokens</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'fragment'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">headers_sent</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$permanent</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'HTTP/1.1 301 Moved permanently.'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
		<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Location: '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$target</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #990000;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'&lt;html&gt;&lt;head&gt;&lt;meta http-equiv=&quot;refresh&quot; content=&quot;0;URL=%s&quot;&gt;&lt;/meta&gt;&lt;/head&gt;&lt;body&gt;&lt;a href=&quot;%s&quot;&gt;%s&lt;/a&gt;&lt;/body&gt;&lt;/html&gt;'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$target</span><span style="color: #339933;">,</span> <span style="color: #000088;">$target</span><span style="color: #339933;">,</span> <span style="color: #000088;">$target</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #990000;">exit</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div><p>Die Funktion nutzt die IDN-Funktionen von <a
href="http://pecl.php.net/package/idn" rel="nofollow" target="_blank">http://pecl.php.net/package/idn</a>.</p> ]]></content:encoded> <wfw:commentRss>http://www.naden.de/blog/php-redirect/feed</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Zeitdifferenz in Worten darstellen</title><link>http://www.naden.de/blog/zeitdifferenz-in-worten-darstellen</link> <comments>http://www.naden.de/blog/zeitdifferenz-in-worten-darstellen#comments</comments> <pubDate>Tue, 14 Apr 2009 12:09:07 +0000</pubDate> <dc:creator>naden</dc:creator> <category><![CDATA[Php]]></category> <category><![CDATA[mktime]]></category> <category><![CDATA[time]]></category> <category><![CDATA[zeitdifferenz]]></category> <category><![CDATA[zeitformat]]></category><guid
isPermaLink="false">http://www.naden.de/blog/zeitdifferenz-in-worten-darstellen</guid> <description><![CDATA[Php Zeitfunktionen gibt es zwar massenweise, doch m&#246;chte man eine Zeitdifferenz in Worten darstellen, so bietet Php daf&#252;r leider kein Zeitformat an.
Die Funktion timelag2words() erledigt diese Aufgabe und liefert beim Aufruf folgende Ausgabe:
Aufrufprintf&#40; 'Vor ca. %s', timelag2words&#40; mktime&#40; 1, 2, 3, 12, 32, 1997 &#41;, null, 6 &#41; &#41;;Ausgabe
Vor ca. 11 Jahren, 3 Monaten, 16 [...]]]></description> <content:encoded><![CDATA[<p>Php Zeitfunktionen gibt es zwar massenweise, doch m&#246;chte man eine Zeitdifferenz in Worten darstellen, so bietet Php daf&#252;r leider kein Zeitformat an.<span
id="more-124"></span></p><p>Die Funktion <em>timelag2words()</em> erledigt diese Aufgabe und liefert beim Aufruf folgende Ausgabe:</p><p><strong>Aufruf</strong></p><div
class="wp_codebox"><table><tr
id="p1249"><td
class="code" id="p124code9"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">printf</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'Vor ca. %s'</span><span style="color: #339933;">,</span> timelag2words<span style="color: #009900;">&#40;</span> <span style="color: #990000;">mktime</span><span style="color: #009900;">&#40;</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">12</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">32</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1997</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">6</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div><p><strong>Ausgabe</strong><br
/> <em>Vor ca. 11 Jahren, 3 Monaten, 16 Tagen, 11 Stunden, 53 Minuten, 51 Sekunden</em></p><p>&#220;ber den Paramater <em>$granularity</em> l&#228;sst sich genau einstellen, welche Zeitkomponenten im Ergebnis enthalten sein sollen. Die Funktion kann alternativ ein unformatiertes Array mit Werten zur&#252;ckliefern.</p><p>Achtung! <em>timelag2words()</em> beachtet keine Schaltjahre und rechnet damit, dass jeder Monat 30 Tage hat. Deshalb macht es Sinn, vor die Ausgabe &#8220;ca.&#8221; zu schreiben.</p><div
class="wp_codebox"><table><tr
id="p12410"><td
class="code" id="p124code10"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
 * timelag2words
 *
 * @param timestamp $from      Von Zeitpunkt
 * @param timestamp $to         Bis Zeitfunkt default jetzt
 * @param integer $granularity  Was soll alles angezeigt werden 1-6
 * @param array  $titles           Titel können in anderer Sprach übergeben werden
 * @param string $return          return type, string default, array sonst
 *
 * @return array
 */</span> 
<span style="color: #000000; font-weight: bold;">function</span> timelag2words<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$from</span><span style="color: #339933;">,</span> <span style="color: #000088;">$to</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">,</span> <span style="color: #000088;">$granularity</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">6</span><span style="color: #339933;">,</span> <span style="color: #000088;">$titles</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">,</span> <span style="color: #000088;">$return</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'string'</span> <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #990000;">is_null</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$titles</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$titles</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
      <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'Jahr'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Monat'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Tag'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Stunde'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Minute'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Sekunde'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
      <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'Jahren'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Monaten'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Tagen'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Stunden'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Minuten'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Sekunden'</span> <span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000088;">$time</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span> <span style="color: #990000;">is_null</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$to</span> <span style="color: #009900;">&#41;</span> ? <span style="color: #990000;">time</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> <span style="color: #000088;">$to</span> <span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$from</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span> <span style="color: #cc66cc;">31536000</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2592000</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">86400</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">3600</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">60</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span> <span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$index</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$delta</span> <span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$current</span> <span style="color: #339933;">=</span> <span style="color: #990000;">intval</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$time</span> <span style="color: #339933;">/</span> <span style="color: #000088;">$delta</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span> <span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$granularity</span> <span style="color: #339933;">&gt;=</span> <span style="color: #000088;">$index</span> <span style="color: #339933;">+</span> <span style="color: #cc66cc;">1</span> <span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$result</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$current</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">' '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$titles</span><span style="color: #009900;">&#91;</span> <span style="color: #000088;">$current</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">1</span> ? <span style="color: #cc66cc;">0</span> <span style="color: #339933;">:</span> <span style="color: #cc66cc;">1</span> <span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span> <span style="color: #000088;">$index</span> <span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
      <span style="color: #000088;">$time</span> <span style="color: #339933;">-=</span> <span style="color: #000088;">$current</span> <span style="color: #339933;">*</span> <span style="color: #000088;">$delta</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #b1b100;">return</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$return</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'string'</span> ? <span style="color: #990000;">implode</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">', '</span><span style="color: #339933;">,</span> <span style="color: #000088;">$result</span> <span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> <span style="color: #000088;">$result</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>]]></content:encoded> <wfw:commentRss>http://www.naden.de/blog/zeitdifferenz-in-worten-darstellen/feed</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Ip Regular Expression Check</title><link>http://www.naden.de/blog/ip-regular-expression-check</link> <comments>http://www.naden.de/blog/ip-regular-expression-check#comments</comments> <pubDate>Sat, 11 Apr 2009 12:13:13 +0000</pubDate> <dc:creator>naden</dc:creator> <category><![CDATA[Php]]></category> <category><![CDATA[ip]]></category> <category><![CDATA[ip check]]></category> <category><![CDATA[ip prüfen]]></category> <category><![CDATA[ip v4]]></category><guid
isPermaLink="false">http://www.naden.de/blog/ip-regular-expression-check</guid> <description><![CDATA[Um eine IP-Adresse im Dot Quoted-Format auf Richtigkeit zu &#252;berpr&#252;fen tut es ein regul&#228;rer Ausdruck alleine nicht, da man mit regul&#228;ren Ausdr&#252;cken leider keine &#8220;gr&#246;&#223;er kleiner&#8221; Tests durchf&#252;hren kann.
Die Funktion isIp()pr&#252;ft zuerst, ob die IP aus vier, durch einen Punkt getrennten Bl&#246;cken aus 1 bis 3-stelligen Zahlen besteht.
Im n&#228;chsten Schritt wird noch abgesichert, dass die [...]]]></description> <content:encoded><![CDATA[<p>Um eine IP-Adresse im Dot Quoted-Format auf Richtigkeit zu &#252;berpr&#252;fen tut es ein regul&#228;rer Ausdruck alleine nicht, da man mit regul&#228;ren Ausdr&#252;cken leider keine &#8220;gr&#246;&#223;er kleiner&#8221; Tests durchf&#252;hren kann.<span
id="more-123"></span></p><p>Die Funktion <em>isIp()</em>pr&#252;ft zuerst, ob die IP aus vier, durch einen Punkt getrennten Bl&#246;cken aus 1 bis 3-stelligen Zahlen besteht.</p><p>Im n&#228;chsten Schritt wird noch abgesichert, dass die gefundenen Zahlen kleiner gleich 255 sind. Ein Zahlenblock einer IP v4 Adresse mu&#223; zwischen 0 und 255 liegen.</p><p>Ein Test auf &#8220;gr&#246;&#223;er gleich&#8221; 0 entf&#228;llt, da schon der regul&#228;re Ausdruck keine negativen Zahlen erlaubt.</p><div
class="wp_codebox"><table><tr
id="p12312"><td
class="code" id="p123code12"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> isIp<span style="color: #009900;">&#40;</span><span style="color: #000088;">$s</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">ereg</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'^([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$s</span><span style="color: #339933;">,</span> <span style="color: #000088;">$tokens</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> 
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$tokens</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">&lt;=</span> <span style="color: #cc66cc;">255</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$tokens</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">&lt;=</span> <span style="color: #cc66cc;">255</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$tokens</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">&lt;=</span> <span style="color: #cc66cc;">255</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$tokens</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">&lt;=</span> <span style="color: #cc66cc;">255</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>]]></content:encoded> <wfw:commentRss>http://www.naden.de/blog/ip-regular-expression-check/feed</wfw:commentRss> <slash:comments>6</slash:comments> </item> <item><title>Sedo Domain Parking</title><link>http://www.naden.de/blog/sedo-domain-parking</link> <comments>http://www.naden.de/blog/sedo-domain-parking#comments</comments> <pubDate>Tue, 07 Apr 2009 18:52:29 +0000</pubDate> <dc:creator>naden</dc:creator> <category><![CDATA[Php]]></category> <category><![CDATA[allgemeines]]></category> <category><![CDATA[domain]]></category> <category><![CDATA[domain parking]]></category> <category><![CDATA[domains]]></category> <category><![CDATA[parking ads]]></category> <category><![CDATA[sedo]]></category><guid
isPermaLink="false">http://www.naden.de/blog/sedo-domain-parking</guid> <description><![CDATA[Wer viele Domains bei Sedo per redirect parken m&#246;chte, der wird sich &#252;ber das folgende PHP Script freuen. Es erkennt automatisch den Domainnamen und leitet auf die Parkingseite um.
Einfach als index.php ins Rootverzeichnis der Domain legen. Fertig!/**
* Simple Sedo-Parking Script by http://www.naden.de
*/
&#160;
$host = strtolower&#40; $_SERVER&#91; 'HTTP_HOST' &#93; &#41;;
&#160;
if&#40; substr&#40; $host, 0, 4 &#41; [...]]]></description> <content:encoded><![CDATA[<p>Wer viele Domains bei Sedo per redirect parken m&#246;chte, der wird sich &#252;ber das folgende PHP Script freuen. Es erkennt automatisch den Domainnamen und leitet auf die Parkingseite um.<span
id="more-121"></span></p><p>Einfach als <em>index.php</em> ins Rootverzeichnis der Domain legen. Fertig!</p><div
class="wp_codebox"><table><tr
id="p12114"><td
class="code" id="p121code14"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
 * Simple Sedo-Parking Script by http://www.naden.de
 */</span>
&nbsp;
<span style="color: #000088;">$host</span> <span style="color: #339933;">=</span> <span style="color: #990000;">strtolower</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span> <span style="color: #0000ff;">'HTTP_HOST'</span> <span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$host</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">4</span> <span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'www.'</span> <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$host</span> <span style="color: #339933;">=</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$host</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">4</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'HTTP/1.1 301 Moved permanently.'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'Location: http://www.sedoparking.de/'</span><span style="color: #339933;">.</span> <span style="color: #000088;">$host</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #990000;">exit</span><span style="color: #339933;">;</span></pre></td></tr></table></div>]]></content:encoded> <wfw:commentRss>http://www.naden.de/blog/sedo-domain-parking/feed</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Farbauswahl auf Lesbarkeit pr&#252;fen</title><link>http://www.naden.de/blog/farbauswahl-auf-lesbarkeit-pruefen</link> <comments>http://www.naden.de/blog/farbauswahl-auf-lesbarkeit-pruefen#comments</comments> <pubDate>Mon, 06 Apr 2009 21:28:57 +0000</pubDate> <dc:creator>naden</dc:creator> <category><![CDATA[Php]]></category> <category><![CDATA[color]]></category> <category><![CDATA[colors]]></category> <category><![CDATA[farben]]></category> <category><![CDATA[hexdec]]></category> <category><![CDATA[kontrast]]></category> <category><![CDATA[lesbarkeit]]></category> <category><![CDATA[rgb]]></category><guid
isPermaLink="false">http://www.naden.de/blog/farbauswahl-auf-lesbarkeit-pruefen</guid> <description><![CDATA[Wenn man eine Anwendung schreibt, wo der Benutzer zwei Farben ausw&#228;hlen mu&#223;, z.B. eine Vorder- und eine Hintergrundfarbe, dann kann es von Interesse sein, dass die gew&#228;hlte Farbkombination auch gut lesbar ist.
Inspiriert von einem in Javascript geschriebenen Colorpicker (leider kenne ich den Autor nicht) habe ich dazu eine PHP-Klasse geschrieben, welche eine statische Methode implementiert [...]]]></description> <content:encoded><![CDATA[<p>Wenn man eine Anwendung schreibt, wo der Benutzer zwei Farben ausw&#228;hlen mu&#223;, z.B. eine Vorder- und eine Hintergrundfarbe, dann kann es von Interesse sein, dass die gew&#228;hlte Farbkombination auch gut lesbar ist.<span
id="more-120"></span></p><p>Inspiriert von einem in Javascript geschriebenen Colorpicker (leider kenne ich den Autor nicht) habe ich dazu eine PHP-Klasse geschrieben, welche eine statische Methode implementiert um genau diese Lesbarkeit zu &#252;berpr&#252;fen. Die Methode erwartet die beiden Farben als RGB-Wert in 6 Zeichen hexadezimal und liefert ein boolsches Ergebnis. Optional kann als dritter Parameter ein Schwellwert angegeben werden um das Ergebnis gegebenenfalls an den pers&#246;nlichen Geschmack anzupassen.</p><div
class="wp_codebox"><table><tr
id="p12016"><td
class="code" id="p120code16"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> ColorDistance
<span style="color: #009900;">&#123;</span>
  <span style="color: #009933; font-style: italic;">/**
   * Wandle einen RGB-Wert in ein Arrray of Integer um
   * @param string $color pointer to rgb string
   */</span>
  <span style="color: #000000; font-weight: bold;">private</span> static <span style="color: #000000; font-weight: bold;">function</span> convert<span style="color: #009900;">&#40;</span> <span style="color: #339933;">&amp;</span><span style="color: #000088;">$color</span> <span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #990000;">array_walk</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$color</span> <span style="color: #339933;">=</span> <span style="color: #990000;">str_split</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$color</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #990000;">create_function</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'&amp;$v'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'$v = hexdec( $v );'</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #009933; font-style: italic;">/**
   * Überprüfe den Farbabstand von zwei RGB-Werten
   * @param string $color1
   * @param string $color2
   * @param integer $delta Schwellwert gibt an, ab wann die Kombination nicht mehr lesbar ist
   * @return boolean
   */</span>
  <span style="color: #000000; font-weight: bold;">public</span> static <span style="color: #000000; font-weight: bold;">function</span> check<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$color1</span><span style="color: #339933;">,</span> <span style="color: #000088;">$color2</span><span style="color: #339933;">,</span> <span style="color: #000088;">$delta</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">190</span> <span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #004000;">convert</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$color1</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #004000;">convert</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$color2</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$n</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$k</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #000088;">$k</span><span style="color: #339933;">&lt;</span> <span style="color: #339933;">=</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span> <span style="color: #000088;">$k</span><span style="color: #339933;">++</span> <span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$n</span> <span style="color: #339933;">+=</span> <span style="color: #990000;">pow</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$color1</span><span style="color: #009900;">&#91;</span> <span style="color: #000088;">$k</span> <span style="color: #009900;">&#93;</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$color2</span><span style="color: #009900;">&#91;</span> <span style="color: #000088;">$k</span> <span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #b1b100;">return</span><span style="color: #009900;">&#40;</span> <span style="color: #990000;">sqrt</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$n</span> <span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #000088;">$delta</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/*
$color1 = '008FD4';
$color2 = '00ADEF';
&nbsp;
var_dump( ColorDistance::check( $color1, $color2 ) );
*/</span></pre></td></tr></table></div><p><a
href="/blog/shorturl/38" class="download">Download</a></p> ]]></content:encoded> <wfw:commentRss>http://www.naden.de/blog/farbauswahl-auf-lesbarkeit-pruefen/feed</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Php suchen ersetzen und HTML-Tags erhalten</title><link>http://www.naden.de/blog/php-suchen-ersetzen-und-html-tags-erhalten</link> <comments>http://www.naden.de/blog/php-suchen-ersetzen-und-html-tags-erhalten#comments</comments> <pubDate>Fri, 03 Apr 2009 09:44:08 +0000</pubDate> <dc:creator>naden</dc:creator> <category><![CDATA[Php]]></category> <category><![CDATA[callback]]></category> <category><![CDATA[create-function]]></category> <category><![CDATA[pattern]]></category> <category><![CDATA[preg-replace]]></category> <category><![CDATA[preg-replace-all]]></category> <category><![CDATA[regex]]></category> <category><![CDATA[replace]]></category> <category><![CDATA[search]]></category><guid
isPermaLink="false">http://www.naden.de/blog/php-suchen-ersetzen-und-html-tags-erhalten</guid> <description><![CDATA[PHP hat so einige Funktion zum suchen und ersetzen von Textstrings mit und ohne die Hilfe von regul&#228;rer Ausdr&#252;cken. Hat man die n&#246;tige Geduld und das Wissen, kann man damit fast alles &#8220;kl&#228;ren und s&#228;ubern&#8221;.
In Blogartikeln, die HTML wie Links und Bilder enthalten, wollte ich zus&#228;tzliche Links einparsen die mit bestimmten Keywords verkn&#252;pft sind.
Das Problem, [...]]]></description> <content:encoded><![CDATA[<p>PHP hat so einige Funktion zum suchen und ersetzen von Textstrings mit und ohne die Hilfe von regul&#228;rer Ausdr&#252;cken. Hat man die n&#246;tige Geduld und das Wissen, kann man damit fast alles &#8220;kl&#228;ren und s&#228;ubern&#8221;.<span
id="more-119"></span></p><p>In Blogartikeln, die HTML wie Links und Bilder enthalten, wollte ich zus&#228;tzliche Links einparsen die mit bestimmten Keywords verkn&#252;pft sind.</p><p>Das Problem, was ich mit einem regul&#228;ren Ausdruck nicht l&#246;sen konnte ist, dass auch Keywords innerhalb von Attributen wie <em>title=&#8221;Keyword&#8221;</em>, <em>&lt;img src=&#8221;&#8230;/keyword/&#8230;&#8221; /&gt;</em> oder <em>&lt;a href=&#8221;"&gt;Keyword&lt;/a&gt;</em> etc. ersetzte wurden.</p><p>Meine L&#246;sung arbeitet mit zwei regul&#228;ren Ausdr&#252;cken. Erst wird der Text an den HTML-Tags in Tokens zerlegt. Diese Tokens werden durchlaufen. Befindet sich der Algorithmus innerhalb eines HTML-Tags, so werden gefundene Keywords ignoriert. Ansonsten ersetzt.</p><p>Da der Text zwingend xml-konform sein muss, werden zuerst HTML-Short-Tags wie <em>&lt;img&gt;</em> und <em>&lt;br&gt;</em> in valides XML umgeformt. Dies wird ganz am Ende des Parsers wieder r&#252;ckg&#228;ngig gemacht.</p><p>Bei sehr schlechtem HTML lohnt sich eventuell der Einsatz von <em>tidy_clean_repair</em>.</p><div
class="wp_codebox"><table><tr
id="p11918"><td
class="code" id="p119code18"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
 * Suchen und ersetzen von Strings wobei HTML-Tags beachtet werden.
 * @param string $search zu suchender string oder callback function
 * @param string $replace zu ersetzender string
 * @param string $buffer zu durchsuchende Zeichenkette
 * @param boolean $ic ignoriere Schreibweise, default true
 * @return string
 */</span>
<span style="color: #000000; font-weight: bold;">function</span> replace<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$search</span><span style="color: #339933;">,</span> <span style="color: #000088;">$replace</span><span style="color: #339933;">,</span> <span style="color: #000088;">$buffer</span><span style="color: #339933;">,</span> <span style="color: #000088;">$ic</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span> <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #009933; font-style: italic;">/**
   * wandle short-tags um, erleichtert das Parsen
   * &lt;img /&gt; =&gt; &lt;img /&gt;, &lt;br /&gt; =&gt; &lt;br /&gt;
   */</span>
  <span style="color: #000088;">$buffer</span> <span style="color: #339933;">=</span> <span style="color: #990000;">preg_replace</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'/&lt;img ([^/&gt;]+)(.*)( ?\/?)&gt;/iU'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'&lt;img \\1\\2/&gt;'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$buffer</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$buffer</span> <span style="color: #339933;">=</span> <span style="color: #990000;">preg_replace</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'/&lt;br ( ?)(.?)/&gt;/iU'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'&lt;br /&gt;'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$buffer</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #009933; font-style: italic;">/**
   * matche alle html-tags
   */</span>
  <span style="color: #990000;">preg_match_all</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'/(&lt; [^&gt;]+&gt;|[^&lt; ]+)/i'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$buffer</span><span style="color: #339933;">,</span> <span style="color: #000088;">$tokens</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009933; font-style: italic;">/**
   * durchlaufe gefundene blöcke
   */</span>
  <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$tokens</span><span style="color: #009900;">&#91;</span> <span style="color: #cc66cc;">0</span> <span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span> <span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$depth</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$tokens</span><span style="color: #009900;">&#91;</span> <span style="color: #cc66cc;">0</span> <span style="color: #009900;">&#93;</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$token</span> <span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #009933; font-style: italic;">/**
       * überprüfe ob wir innerhalb eines &lt;img /&gt; oder &lt;a&gt; tags sind
       */</span>
      <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$token</span><span style="color: #009900;">&#91;</span> <span style="color: #cc66cc;">0</span> <span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'&lt; '</span> <span style="color: #339933;">||</span> <span style="color: #000088;">$depth</span> <span style="color: #339933;">!=</span> <span style="color: #cc66cc;">0</span> <span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$data</span> <span style="color: #339933;">.=</span> <span style="color: #000088;">$token</span><span style="color: #339933;">;</span>
        <span style="color: #009933; font-style: italic;">/**
         * tag wird geöffnet
         */</span>
        <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$token</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">5</span> <span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'&lt;img '</span> <span style="color: #339933;">||</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$token</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">3</span> <span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'&lt;a '</span> <span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
          <span style="color: #000088;">$depth</span> <span style="color: #339933;">++;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #009933; font-style: italic;">/**
         * tag wird geschlossen
         */</span>
        <span style="color: #b1b100;">elseif</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$token</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'&lt;/img&gt;'</span> <span style="color: #339933;">||</span> <span style="color: #000088;">$token</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'&lt;/a&gt;'</span> <span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
          <span style="color: #000088;">$depth</span> <span style="color: #339933;">--;</span>
        <span style="color: #009900;">&#125;</span>
      <span style="color: #009900;">&#125;</span>
      <span style="color: #b1b100;">else</span>
      <span style="color: #009900;">&#123;</span>
        <span style="color: #009933; font-style: italic;">/**
         * eigentlichen string suchen ...
         */</span>
        <span style="color: #990000;">preg_match_all</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'/\b'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$search</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'\b/'</span> <span style="color: #339933;">.</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$ic</span> ? <span style="color: #0000ff;">'i'</span> <span style="color: #339933;">:</span> <span style="color: #0000ff;">''</span> <span style="color: #009900;">&#41;</span> <span style="color: #339933;">,</span> <span style="color: #000088;">$token</span><span style="color: #339933;">,</span> <span style="color: #000088;">$matches</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$matches</span><span style="color: #009900;">&#91;</span> <span style="color: #cc66cc;">0</span> <span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span> <span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
          <span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$matches</span><span style="color: #009900;">&#91;</span> <span style="color: #cc66cc;">0</span> <span style="color: #009900;">&#93;</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$match</span> <span style="color: #009900;">&#41;</span>
          <span style="color: #009900;">&#123;</span>
            <span style="color: #009933; font-style: italic;">/**
             * ... und ersetzen, callback möglich
             */</span>
            <span style="color: #000088;">$token</span> <span style="color: #339933;">=</span> <span style="color: #990000;">preg_replace</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;/<span style="color: #006699; font-weight: bold;">$match</span>/&quot;</span><span style="color: #339933;">,</span> <span style="color: #990000;">is_callable</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$replace</span> <span style="color: #009900;">&#41;</span> ? <span style="color: #000088;">$replace</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$match</span> <span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> <span style="color: #000088;">$replace</span><span style="color: #339933;">,</span> <span style="color: #000088;">$token</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000088;">$data</span> <span style="color: #339933;">.=</span> <span style="color: #000088;">$token</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000088;">$buffer</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$data</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #990000;">unset</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$data</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #009933; font-style: italic;">/**
   * wandle anfangs geänderte html tags zurück
   */</span>
  <span style="color: #000088;">$buffer</span> <span style="color: #339933;">=</span> <span style="color: #990000;">str_replace</span><span style="color: #009900;">&#40;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'&gt;'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'&lt;br /&gt;'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">' /&gt;'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'&lt;br /&gt;'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$buffer</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #b1b100;">return</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$buffer</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * test
 */</span>
<span style="color: #000088;">$buffer</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'&lt;p&gt;&lt;img src=&quot;/teststring&quot; title=&quot;Titel: Ein TestString&quot; alt=&quot;Bild: TestString&quot; /&gt; dieser TestString wird ersetzte.&lt;br /&gt;&lt;strong&gt;dieser TestString auch&lt;/strong&gt; &lt;a href=&quot;&quot; title=&quot;Ziel TestString&quot;&gt;hier geht es zu TestString&lt;/a&gt;.&lt;/p&gt;'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * replace
 */</span>
<span style="color: #000088;">$buffer</span> <span style="color: #339933;">=</span> replace<span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'teststring'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'&lt;a href=&quot;&quot;&gt;Gefunden&lt;/a&gt;'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$buffer</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * replace mit callback
 */</span>
<span style="color: #000088;">$buffer</span> <span style="color: #339933;">=</span> replace<span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'teststring'</span><span style="color: #339933;">,</span> <span style="color: #990000;">create_function</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'$s'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'return( strtoupper( $s ) );'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$buffer</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">print</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$buffer</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div><p><a
href="/blog/wp-content/download/replace.phps" rel="nofollow" class="download" target="_blank">Download</a></p><p>Verbesserungsvorschl&#228;ge sind willkommen!</p> ]]></content:encoded> <wfw:commentRss>http://www.naden.de/blog/php-suchen-ersetzen-und-html-tags-erhalten/feed</wfw:commentRss> <slash:comments>3</slash:comments> </item> <item><title>PHP Debug Output</title><link>http://www.naden.de/blog/php-debug-output</link> <comments>http://www.naden.de/blog/php-debug-output#comments</comments> <pubDate>Thu, 02 Apr 2009 10:51:55 +0000</pubDate> <dc:creator>naden</dc:creator> <category><![CDATA[Php]]></category> <category><![CDATA[debug]]></category> <category><![CDATA[debugging]]></category> <category><![CDATA[dump]]></category> <category><![CDATA[ob-get-clean]]></category> <category><![CDATA[ob-start]]></category> <category><![CDATA[output]]></category> <category><![CDATA[print-r]]></category> <category><![CDATA[printf]]></category> <category><![CDATA[var-dump]]></category> <category><![CDATA[var-export]]></category><guid
isPermaLink="false">http://www.naden.de/blog/php-debug-output</guid> <description><![CDATA[Grunds&#228;tzlich sollte man nie an einem Livesystem arbeiten. In der Praxis kommt das leider immer mal wieder vor, wenn man kein komplett gespiegeltes System zum Testen hat.
Den Debug Output vor dem Besucher zu verstecken ist oberstes Gebot. Dazu zwei simple L&#246;sungen:
print_r()printf&#40; '&#60;!-- %s --&#62;', print_r&#40; $var, true &#41; &#41;;var_dump()ob_start&#40;&#41;;
var_dump&#40; $var &#41;;
printf&#40; '&#60;!-- %s --&#62;', ob_get_clean&#40;&#41; [...]]]></description> <content:encoded><![CDATA[<p>Grunds&#228;tzlich sollte man nie an einem Livesystem arbeiten. In der Praxis kommt das leider immer mal wieder vor, wenn man kein komplett gespiegeltes System zum Testen hat.<span
id="more-118"></span></p><p>Den Debug Output vor dem Besucher zu verstecken ist oberstes Gebot. Dazu zwei simple L&#246;sungen:</p><p><strong>print_r()</strong></p><div
class="wp_codebox"><table><tr
id="p11822"><td
class="code" id="p118code22"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">printf</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'&lt;!-- %s --&gt;'</span><span style="color: #339933;">,</span> <span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$var</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div><p><strong>var_dump()</strong></p><div
class="wp_codebox"><table><tr
id="p11823"><td
class="code" id="p118code23"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">ob_start</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">var_dump</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$var</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">printf</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'&lt;!-- %s --&gt;'</span><span style="color: #339933;">,</span> <span style="color: #990000;">ob_get_clean</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div><p><strong>var_export()</strong></p><div
class="wp_codebox"><table><tr
id="p11824"><td
class="code" id="p118code24"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">printf</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'&lt;!-- %s --&gt;'</span><span style="color: #339933;">,</span> <span style="color: #990000;">var_export</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$var</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div><p>By <a
href="#comment-41717">killerbees</a> liefert allerdings PHP-Code zur&#252;ck.</p><p>Die L&#246;sung mit <em>print_r()</em> gef&#228;llt etwas besser, da sie nur eine einzige Zeile ben&#246;tigt.</p> ]]></content:encoded> <wfw:commentRss>http://www.naden.de/blog/php-debug-output/feed</wfw:commentRss> <slash:comments>6</slash:comments> </item> <item><title>Omnimage &#8211; PHP Image function collection</title><link>http://www.naden.de/blog/omnimage-php-image-function-collection</link> <comments>http://www.naden.de/blog/omnimage-php-image-function-collection#comments</comments> <pubDate>Sun, 04 Jan 2009 20:43:20 +0000</pubDate> <dc:creator>naden</dc:creator> <category><![CDATA[Php]]></category> <category><![CDATA[circle]]></category> <category><![CDATA[gd]]></category> <category><![CDATA[gd2]]></category> <category><![CDATA[image]]></category> <category><![CDATA[rectangle]]></category> <category><![CDATA[transparent]]></category><guid
isPermaLink="false">http://www.naden.de/blog/omnimage-php-image-function-collection</guid> <description><![CDATA[Es muss nicht immer Imagemagick sein. Oft braucht man einige simple Funktionen zur Bildmanipulation, die in den Grundfunktionen von PHP nicht enthalten sind.
Konkret hatte ich den Fall, dass ich Rechtecke ben&#246;tigte die abgerundete Ecken hatten und das in allen erdenklichen Gr&#246;&#223;en.
Dazu habe ich die folgenden drei Funktionen gebaut und in einer Bibliothek zusammengefasst.imagefilledroundedrectangel&#40; &#38;$img, $x, [...]]]></description> <content:encoded><![CDATA[<p>Es muss nicht immer Imagemagick sein. Oft braucht man einige simple Funktionen zur Bildmanipulation, die in den Grundfunktionen von PHP nicht enthalten sind.<span
id="more-99"></span></p><p>Konkret hatte ich den Fall, dass ich Rechtecke ben&#246;tigte die abgerundete Ecken hatten und das in allen erdenklichen Gr&#246;&#223;en.</p><p>Dazu habe ich die folgenden drei Funktionen gebaut und in einer Bibliothek zusammengefasst.</p><div
class="wp_codebox"><table><tr
id="p9926"><td
class="code" id="p99code26"><pre class="php" style="font-family:monospace;">imagefilledroundedrectangel<span style="color: #009900;">&#40;</span> <span style="color: #339933;">&amp;</span><span style="color: #000088;">$img</span><span style="color: #339933;">,</span> <span style="color: #000088;">$x</span><span style="color: #339933;">,</span> <span style="color: #000088;">$y</span><span style="color: #339933;">,</span> <span style="color: #000088;">$w</span><span style="color: #339933;">,</span> <span style="color: #000088;">$h</span><span style="color: #339933;">,</span> <span style="color: #000088;">$curve</span><span style="color: #339933;">,</span> <span style="color: #000088;">$color</span> <span style="color: #009900;">&#41;</span>
&nbsp;
imageroundedrectangel<span style="color: #009900;">&#40;</span> <span style="color: #339933;">&amp;</span><span style="color: #000088;">$img</span><span style="color: #339933;">,</span> <span style="color: #000088;">$x</span><span style="color: #339933;">,</span> <span style="color: #000088;">$y</span><span style="color: #339933;">,</span> <span style="color: #000088;">$w</span><span style="color: #339933;">,</span> <span style="color: #000088;">$h</span><span style="color: #339933;">,</span> <span style="color: #000088;">$curve</span><span style="color: #339933;">,</span> <span style="color: #000088;">$color</span><span style="color: #339933;">,</span> <span style="color: #000088;">$border</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span> <span style="color: #009900;">&#41;</span>
&nbsp;
imagefilledroundedrectangelborder<span style="color: #009900;">&#40;</span> <span style="color: #339933;">&amp;</span><span style="color: #000088;">$img</span><span style="color: #339933;">,</span> <span style="color: #000088;">$x</span><span style="color: #339933;">,</span> <span style="color: #000088;">$y</span><span style="color: #339933;">,</span> <span style="color: #000088;">$w</span><span style="color: #339933;">,</span> <span style="color: #000088;">$h</span><span style="color: #339933;">,</span> <span style="color: #000088;">$curve</span><span style="color: #339933;">,</span> <span style="color: #000088;">$color1</span><span style="color: #339933;">,</span> <span style="color: #000088;">$color2</span><span style="color: #339933;">,</span> <span style="color: #000088;">$border</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span> <span style="color: #009900;">&#41;</span></pre></td></tr></table></div><p>Die Ausgabe kann wie folgt aussehen:</p><p><img
src="http://www.naden.de/blog/wp-content/uploads/omnimage.gif" alt="Omnimage Screenshot" /></p><p>Ich werde bei Zeiten die Bibiothek um diverse Funktionen erweitern, die sich &#252;ber die Jahre angesammelt haben.</p><p><a
href="/blog/shorturl/33" target="_blank" rel="nofollow" class="download">Download</a> v0.1</p> ]]></content:encoded> <wfw:commentRss>http://www.naden.de/blog/omnimage-php-image-function-collection/feed</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Linkfetcher &#8211; Mit PHP Links extrahieren</title><link>http://www.naden.de/blog/linkfetcher-mit-php-links-extrahieren</link> <comments>http://www.naden.de/blog/linkfetcher-mit-php-links-extrahieren#comments</comments> <pubDate>Sun, 20 Jul 2008 17:17:44 +0000</pubDate> <dc:creator>naden</dc:creator> <category><![CDATA[Php]]></category> <category><![CDATA[link extracktor class]]></category> <category><![CDATA[link fetcher]]></category> <category><![CDATA[linkfetcher class]]></category> <category><![CDATA[php link extractor]]></category><guid
isPermaLink="false">http://www.naden.de/blog/linkfetcher-mit-php-links-extrahieren</guid> <description><![CDATA[Um aus einer Webseite alle Links zu extrahieren gibt es schon so einige L&#246;sungen im Netz. Die meisten basieren auf einigen simplen regul&#228;ren Ausdr&#252;cken. Da ich f&#252;r ein aktuelles Projekt eine M&#246;glichkeit brauchte um zus&#228;tzlich zu den Links auch alle Attribute, wie &#8220;title&#8221;, &#8220;rel&#8221;, &#8220;target&#8221; &#8230; etc. zu erhalten, habe ich dazu eine Klasse geschrieben.
Linkfetcher [...]]]></description> <content:encoded><![CDATA[<p>Um aus einer Webseite alle Links zu extrahieren gibt es schon so einige L&#246;sungen im Netz. Die meisten basieren auf einigen simplen regul&#228;ren Ausdr&#252;cken. Da ich f&#252;r ein aktuelles Projekt eine M&#246;glichkeit brauchte um zus&#228;tzlich zu den Links auch alle Attribute, wie &#8220;title&#8221;, &#8220;rel&#8221;, &#8220;target&#8221; &#8230; etc. zu erhalten, habe ich dazu eine Klasse geschrieben.<span
id="more-92"></span></p><p>Linkfetcher ben&#246;tigt zwingend PHP 5 mit diversen Erweiterungen wie domobject, xpath, mb string &#8230; und liefert alle gefunden Links als array of stdClass mit den folgenden Attributen:</p><p><strong>Attribute</strong><br
/> &#8216;rel&#8217;, &#8216;accesskey&#8217;, &#8216;charset&#8217;, &#8216;coords&#8217;, &#8216;href&#8217;,  &#8216;hreflang&#8217;, &#8216;name&#8217;, &#8216;rev&#8217;, &#8217;shape&#8217;, &#8216;tabindex&#8217;</p><p><strong>Events</strong><br
/> &#8216;onblur&#8217;, &#8216;onfocus&#8217;, &#8216;onclick&#8217;, &#8216;ondblclick&#8217;, &#8216;onmousedown&#8217;, &#8216;onmouseup&#8217;, &#8216;onmouseover&#8217;, &#8216;onmousemove&#8217;, &#8216;onmouseout&#8217;, &#8216;onkeypress&#8217;, &#8216;onkeydown&#8217;, &#8216;onkeyup&#8217;</p><p><strong>Universalattribute:</strong><br
/> &#8216;id&#8217;, &#8216;class&#8217;, &#8217;style&#8217;, &#8216;title&#8217;</p><p><strong>Sprachattribute:</strong><br
/> &#8216;dir&#8217;, &#8216;lang&#8217;</p><p>Nicht gefundene Attribute werden als &#8220;&#8221; leerer String zur&#252;ckgegeben. Die Class-Properties nennen sich genau wie die Attribute in der HTML-Spezifikation. Einzig der Linktext wird in &#8220;text&#8221; abgelegt und kann &#252;ber <em>$link->text</em> abgerufen werden.</p><p>Die folgenden Methoden sind implementiert:</p><p><strong>GetLinks( $url );</strong> extrahiert aus der angegebenen Url s&#228;mtliche Links inklusive aller Attribute.</p><p><strong>GetLink( $link, $exact );</strong> &#252;berpr&#252;ft, ob $link beim letzen Aufruf von <em>GetLinks(&#8230;)</em> gefunden wurde. Der optionale Parameter <em>exact</em> legt fest, ob der Link genau &#252;bereinstimmen muss oder ob es reicht, wenn ein Link gleich beginnt.</p><p><strong>Beispiel #1:</strong><br
/> Der Codeschnipsel holt s&#228;mtliche Links von www.google.de und gibt sie inklusive aller Attribute aus.</p><div
class="wp_codebox"><table><tr
id="p9230"><td
class="code" id="p92code30"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">include_once</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'linkfetcher.class.php'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$Linkfetcher</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Linkfetcher<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$Linkfetcher</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">GetLinks</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'http://www.google.de'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #990000;">var_dump</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$Linkfetcher</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">links</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div><p><strong>Beispiel #2:</strong><br
/> In diesem Beispiel werden wieder alle Links von www.google.de extrahiert. Anschliessend wird &#252;berpr&#252;ft, ob der Link &#8220;http://images.google.de&#8221; in der Ergebnismenge enthalten ist. Da <em>$exact</em> auf <em>false</em> gesetzt wurde, gen&#252;gt es, wenn ein Link mit &#8220;http://images.google.de&#8221; beginnt.<br
/> Bei Erfolg wird der gefundene Link als <a
href="http://www.naden.de/blog/tag/stdclass">stdClass</a> Object zur&#252;ckgeliefert. False sonst.</p><div
class="wp_codebox"><table><tr
id="p9231"><td
class="code" id="p92code31"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">include_once</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'linkfetcher.class.php'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$Linkfetcher</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Linkfetcher<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$Linkfetcher</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">GetLinks</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'http://www.google.de'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #990000;">var_dump</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$Linkfetcher</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">GetLink</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'http://images.google.de'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">false</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div><p><strong>Beispiel #3:</strong><br
/> Hole alle Links von www.google.de mit nur den Attributen <em>href, rel und title</em></p><div
class="wp_codebox"><table><tr
id="p9232"><td
class="code" id="p92code32"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">include_once</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'linkfetcher.class.php'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$Linkfetcher</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Linkfetcher<span style="color: #009900;">&#40;</span> 
 <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'attributes'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'href'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'rel'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'title'</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$Linkfetcher</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">GetLinks</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'http://www.google.de'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #990000;">var_dump</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$Linkfetcher</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">links</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div><p><ins
datetime="2008-07-25T09:50:52+00:00">Update:</ins> Ein kleiner Fehler in einem regul&#228;ren Ausdruck wurde beseitigt.</p><p><a
href="/blog/shorturl/32" target="_blank" rel="nofollow" class="download">Download</a> v1.1</p> ]]></content:encoded> <wfw:commentRss>http://www.naden.de/blog/linkfetcher-mit-php-links-extrahieren/feed</wfw:commentRss> <slash:comments>8</slash:comments> </item> </channel> </rss>
<!-- This site's performance optimized by W3 Total Cache. Dramatically improve the speed and reliability of your blog!

Learn more about our WordPress Plugins: http://www.w3-edge.com/wordpress-plugins/

Minified using disk
Page Caching using disk (user agent is rejected)
Database Caching 30/38 queries in 0.100 seconds using disk

Served from: arwen @ 2010-07-31 05:08:36 -->