<!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>Find the first occurrence of a string</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.strspn.html">strspn</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.strtok.html">strtok</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.strstr" class="refentry"> <div class="refnamediv"> <h1 class="refname">strstr</h1> <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">strstr</span> — <span class="dc-title">Find the first occurrence of a string</span></p> </div> <div class="refsect1 description" id="refsect1-function.strstr-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">string</span> <span class="methodname"><strong>strstr</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 part of <em><code class="parameter">haystack</code></em> string starting from and including the first occurrence of <em><code class="parameter">needle</code></em> to the end of <em><code class="parameter">haystack</code></em>. </p> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> This function is case-sensitive. For case-insensitive searches, use <span class="function"><a href="function.stristr.html" class="function">stristr()</a></span>. </p> </p></blockquote> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> If you only want to determine if a particular <em><code class="parameter">needle</code></em> occurs within <em><code class="parameter">haystack</code></em>, use the faster and less memory intensive function <span class="function"><a href="function.strpos.html" class="function">strpos()</a></span> instead. </p> </p></blockquote> </div> <div class="refsect1 parameters" id="refsect1-function.strstr-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 input string. </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>strstr()</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 the needle). </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-function.strstr-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns the portion of string, or <strong><code>FALSE</code></strong> if <em><code class="parameter">needle</code></em> is not found. </p> </div> <div class="refsect1 changelog" id="refsect1-function.strstr-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>strstr()</strong></span> was made binary safe. </td> </tr> </tbody> </table> </p> </div> <div class="refsect1 examples" id="refsect1-function.strstr-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-4904"> <p><strong>Example #1 <span class="function"><strong>strstr()</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">'name@example.com'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$domain </span><span style="color: #007700">= </span><span style="color: #0000BB">strstr</span><span style="color: #007700">(</span><span style="color: #0000BB">$email</span><span style="color: #007700">, </span><span style="color: #DD0000">'@'</span><span style="color: #007700">);<br />echo </span><span style="color: #0000BB">$domain</span><span style="color: #007700">; </span><span style="color: #FF8000">// prints @example.com<br /><br /></span><span style="color: #0000BB">$user </span><span style="color: #007700">= </span><span style="color: #0000BB">strstr</span><span style="color: #007700">(</span><span style="color: #0000BB">$email</span><span style="color: #007700">, </span><span style="color: #DD0000">'@'</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<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">$user</span><span style="color: #007700">; </span><span style="color: #FF8000">// prints name<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> </div> <div class="refsect1 seealso" id="refsect1-function.strstr-seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"> <span class="function"><a href="function.stristr.html" class="function" rel="rdfs-seeAlso">stristr()</a> - Case-insensitive strstr</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.strpos.html" class="function" rel="rdfs-seeAlso">strpos()</a> - Find the position of the first occurrence of a 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.strspn.html">strspn</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.strtok.html">strtok</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>