<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Case-insensitive strstr</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.stripslashes.html">stripslashes</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.strlen.html">strlen</a></div> <div class="up"><a href="ref.strings.html">String Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="function.stristr" class="refentry"> <div class="refnamediv"> <h1 class="refname">stristr</h1> <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">stristr</span> — <span class="dc-title">Case-insensitive <span class="function"><a href="function.strstr.html" class="function">strstr()</a></span></span></p> </div> <div class="refsect1 description" id="refsect1-function.stristr-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">string</span> <span class="methodname"><strong>stristr</strong></span> ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$haystack</code></span> , <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$needle</code></span> [, <span class="methodparam"><span class="type">bool</span> <code class="parameter">$before_needle</code><span class="initializer"> = false</span></span> ] )</div> <p class="para rdfs-comment"> Returns all of <em><code class="parameter">haystack</code></em> starting from and including the first occurrence of <em><code class="parameter">needle</code></em> to the end. </p> </div> <div class="refsect1 parameters" id="refsect1-function.stristr-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><em><code class="parameter">haystack</code></em></span> <dd> <p class="para"> The string to search in </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">needle</code></em></span> <dd> <p class="para"> If <em><code class="parameter">needle</code></em> is not a string, it is converted to an integer and applied as the ordinal value of a character. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">before_needle</code></em></span> <dd> <p class="para"> If <strong><code>TRUE</code></strong>, <span class="function"><strong>stristr()</strong></span> returns the part of the <em><code class="parameter">haystack</code></em> before the first occurrence of the <em><code class="parameter">needle</code></em> (excluding needle). </p> </dd> </dt> </dl> </p> <p class="para"> <em><code class="parameter">needle</code></em> and <em><code class="parameter">haystack</code></em> are examined in a case-insensitive manner. </p> </div> <div class="refsect1 returnvalues" id="refsect1-function.stristr-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns the matched substring. If <em><code class="parameter">needle</code></em> is not found, returns <strong><code>FALSE</code></strong>. </p> </div> <div class="refsect1 changelog" id="refsect1-function.stristr-changelog"> <h3 class="title">Changelog</h3> <p class="para"> <table class="doctable informaltable"> <thead> <tr> <th>Version</th> <th>Description</th> </tr> </thead> <tbody class="tbody"> <tr> <td>5.3.0</td> <td> Added the optional parameter <em><code class="parameter">before_needle</code></em>. </td> </tr> <tr> <td>4.3.0</td> <td> <span class="function"><strong>stristr()</strong></span> was made binary safe. </td> </tr> </tbody> </table> </p> </div> <div class="refsect1 examples" id="refsect1-function.stristr-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-4890"> <p><strong>Example #1 <span class="function"><strong>stristr()</strong></span> example</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /> $email </span><span style="color: #007700">= </span><span style="color: #DD0000">'USER@EXAMPLE.com'</span><span style="color: #007700">;<br /> echo </span><span style="color: #0000BB">stristr</span><span style="color: #007700">(</span><span style="color: #0000BB">$email</span><span style="color: #007700">, </span><span style="color: #DD0000">'e'</span><span style="color: #007700">); </span><span style="color: #FF8000">// outputs ER@EXAMPLE.com<br /> </span><span style="color: #007700">echo </span><span style="color: #0000BB">stristr</span><span style="color: #007700">(</span><span style="color: #0000BB">$email</span><span style="color: #007700">, </span><span style="color: #DD0000">'e'</span><span style="color: #007700">, </span><span style="color: #0000BB">true</span><span style="color: #007700">); </span><span style="color: #FF8000">// As of PHP 5.3.0, outputs US<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> <p class="para"> <div class="example" id="example-4891"> <p><strong>Example #2 Testing if a string is found or not</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /> $string </span><span style="color: #007700">= </span><span style="color: #DD0000">'Hello World!'</span><span style="color: #007700">;<br /> if(</span><span style="color: #0000BB">stristr</span><span style="color: #007700">(</span><span style="color: #0000BB">$string</span><span style="color: #007700">, </span><span style="color: #DD0000">'earth'</span><span style="color: #007700">) === </span><span style="color: #0000BB">FALSE</span><span style="color: #007700">) {<br /> echo </span><span style="color: #DD0000">'"earth" not found in string'</span><span style="color: #007700">;<br /> }<br /></span><span style="color: #FF8000">// outputs: "earth" not found in string<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> <p class="para"> <div class="example" id="example-4892"> <p><strong>Example #3 Using a non "string" needle</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /> $string </span><span style="color: #007700">= </span><span style="color: #DD0000">'APPLE'</span><span style="color: #007700">;<br /> echo </span><span style="color: #0000BB">stristr</span><span style="color: #007700">(</span><span style="color: #0000BB">$string</span><span style="color: #007700">, </span><span style="color: #0000BB">97</span><span style="color: #007700">); </span><span style="color: #FF8000">// 97 = lowercase a<br />// outputs: APPLE<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> </div> <div class="refsect1 notes" id="refsect1-function.stristr-notes"> <h3 class="title">Notes</h3> <blockquote class="note"><p><strong class="note">Note</strong>: <span class="simpara">This function is binary-safe.</span></p></blockquote> </div> <div class="refsect1 seealso" id="refsect1-function.stristr-seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"> <span class="function"><a href="function.strstr.html" class="function" rel="rdfs-seeAlso">strstr()</a> - Find the first occurrence of a string</span></li> <li class="member"> <span class="function"><a href="function.strrchr.html" class="function" rel="rdfs-seeAlso">strrchr()</a> - Find the last occurrence of a character in a string</span></li> <li class="member"> <span class="function"><a href="function.stripos.html" class="function" rel="rdfs-seeAlso">stripos()</a> - Find the position of the first occurrence of a case-insensitive substring in a string</span></li> <li class="member"> <span class="function"><a href="function.strpbrk.html" class="function" rel="rdfs-seeAlso">strpbrk()</a> - Search a string for any of a set of characters</span></li> <li class="member"> <span class="function"><a href="function.preg-match.html" class="function" rel="rdfs-seeAlso">preg_match()</a> - Perform a regular expression match</span></li> </ul> </p> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.stripslashes.html">stripslashes</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.strlen.html">strlen</a></div> <div class="up"><a href="ref.strings.html">String Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>