<?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>Der Informatikblog &#187; substr_count</title>
	<atom:link href="http://www.informatik-blog.net/tag/substr_count/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.informatik-blog.net</link>
	<description>Informatik &#38; Co.</description>
	<lastBuildDate>Mon, 28 Jun 2010 19:43:30 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>substr_count() statt strpos()</title>
		<link>http://www.informatik-blog.net/2009/01/22/substr_count-statt-strpos/</link>
		<comments>http://www.informatik-blog.net/2009/01/22/substr_count-statt-strpos/#comments</comments>
		<pubDate>Thu, 22 Jan 2009 15:44:54 +0000</pubDate>
		<dc:creator>bleed_ch</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[position]]></category>
		<category><![CDATA[strpos]]></category>
		<category><![CDATA[substr_count]]></category>

		<guid isPermaLink="false">http://www.informatik-blog.net/?p=44</guid>
		<description><![CDATA[Immer wieder wird in Foren oder im IRC strpos() empfohlen um zu prüfen ob etwas in einem String vorkommt. Sehen wir uns einmal an warum substr_count() besser dazu geeignet ist: $str = &#34;ich bin ein teststring&#34;; &#160; if &#40;strpos&#40;$str, &#34;bin&#34;&#41;&#41; &#123; // liefert die Position zurück, Integer: 4 // Dieser Block wird ausgeführt. &#125; else [...]]]></description>
			<content:encoded><![CDATA[
<!-- google_ad_section_start -->
<p>Immer wieder wird in Foren oder im IRC strpos() empfohlen um zu prüfen ob etwas in einem String vorkommt. <span id="more-44"></span> Sehen wir uns einmal an warum substr_count() besser dazu geeignet ist:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$str</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;ich bin ein teststring&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">strpos</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$str</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;bin&quot;</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;">// liefert die Position zurück, Integer: 4</span>
	<span style="color: #666666; font-style: italic;">// Dieser Block wird ausgeführt.</span>
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Funktioniert ganz gut. Doch was passiert wenn wir nach &#8220;ich&#8221; suchen?</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$str</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;ich bin ein teststring&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">strpos</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$str</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;ich&quot;</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;">// In dem Fall Integer: 0</span>
&nbsp;
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #666666; font-style: italic;">// Dieser Block wird ausgeführt.</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Was ist passiert? Wir sehen im zweiten Beispiel, dass der else-Teil ausgeführt wird obwohl ein Vorkomniss gefunden wurde. Das liegt ganz einfach daran, dass strpos() eine Positionsangabe zurückliefert und wie wir alle wissen ist 0 für den Computer die erste Zahl. Da der Integerwert 0 aber auch für bool(false) steht, springt er in den else-Teil. Wir müssen bei strpos() also nicht nur den Wert sondern auch den Typ prüfen denn diese Funktion kann je nach Ergebniss 0 oder false zurückliefern was nicht das gleiche ist. Korrekt wäre also:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$str</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;ich bin ein teststring&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">strpos</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$str</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;ich&quot;</span><span style="color: #009900;">&#41;</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;">&#123;</span> <span style="color: #666666; font-style: italic;">// Hier auch Integer: 0</span>
	<span style="color: #666666; font-style: italic;">// Dieser Block wird ausgeführt.</span>
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Fazit: Wenn man einfach nur wissen will ob sich etwas in einem String befindet, sollte man substr_count() benutzen. Wer jedoch die Position braucht oder Gewicht auf (meiner Meinung nach vernachlässigbarer) Performance legt kann auch strpos() nutzen, sollte allerdings nicht vergessen den Typ der Rückgabe zu prüfen.</p>
<!-- google_ad_section_end -->
]]></content:encoded>
			<wfw:commentRss>http://www.informatik-blog.net/2009/01/22/substr_count-statt-strpos/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
