<?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>birghtyoursite &#187; php</title>
	<atom:link href="http://www.brightyoursite.com/blog/category/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.brightyoursite.com/blog</link>
	<description>Bright your site</description>
	<lastBuildDate>Mon, 19 Dec 2011 09:39:57 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>php check string is contains a substring</title>
		<link>http://www.brightyoursite.com/blog/2010/05/23/php-check-string-is-contains-a-substring/</link>
		<comments>http://www.brightyoursite.com/blog/2010/05/23/php-check-string-is-contains-a-substring/#comments</comments>
		<pubDate>Sun, 23 May 2010 17:17:21 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.brightyoursite.com/blog/?p=405</guid>
		<description><![CDATA[Use php to check a string contains substring there maybe many ways, the strpos maybe the most used way, but it&#8217;s strange, it not work for me why? That&#8217;s what i want to say: the thing you need take care while use strpos . what&#8217;s strpos Check the php Manual: &#34; strpos — Find position [...]]]></description>
			<content:encoded><![CDATA[<p>
Use php to check a string contains substring there maybe many ways, the  strpos maybe the most used way, but it&#8217;s strange, it not work for me why? That&#8217;s what i want to say:<b> the thing you need take care while use strpos </b>.
</p>
<p><span id="more-405"></span></p>
<h3>what&#8217;s strpos</h3>
<p>
Check the php Manual: &quot; strpos — Find position of first occurrence of a string &quot;</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">int <span style="color: #990000;">strpos</span>  <span style="color: #009900;">&#40;</span>  string <span style="color: #000088;">$haystack</span>  <span style="color: #339933;">,</span>  mixed <span style="color: #000088;">$needle</span>  <span style="color: #009900;">&#91;</span><span style="color: #339933;">,</span>  int <span style="color: #000088;">$offset</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span>  <span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#41;</span></pre></div></div>

<p>and for the return value:<br />
&quot;<strong>Returns the position as an integer. If needle  is not found, strpos() will return boolean FALSE. </strong>&quot;
</p>
<p>
Here is how i use this:</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;">'google.com'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$find</span><span style="color: #339933;">=</span> <span style="color: #0000ff;">'gle.'</span><span style="color: #339933;">;</span>
<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: #000088;">$find</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;find &quot;</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: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;not find&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Run it,it seems works correctly.Do you find the problem? For this sample,It work but let&#8217;s try this one:</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;">'google.com'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$find</span><span style="color: #339933;">=</span> <span style="color: #0000ff;">'google.'</span><span style="color: #339933;">;</span>
<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: #000088;">$find</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;find &quot;</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: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;not find&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>It&#8217;s show &quot;not find&quot;, why? the strpos return 0, not 1 . so even the substring found , it&#8217;s still show &quot;not find&quot;
</p>
<h3>the right way for strpos</h3>
<p>
Since we know <b>strpos</b> will give  a numeric value if found else it is null or false,so let&#8217;s see the right way:</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;">'google.com'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$find</span><span style="color: #339933;">=</span> <span style="color: #0000ff;">'google.'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">is_numeric</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: #000088;">$find</span><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: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;find &quot;</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: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;not find&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Or</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;">'google.com'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$find</span><span style="color: #339933;">=</span> <span style="color: #0000ff;">'google.'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$isfound</span><span style="color: #339933;">=</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: #000088;">$find</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$isfound</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: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;find &quot;</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: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;not find&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>So you know more about strpos now,Happy reading!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.brightyoursite.com/blog/2010/05/23/php-check-string-is-contains-a-substring/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>

