<!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 position of the first occurrence of a case-insensitive substring in a string</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.stripcslashes.html">stripcslashes</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.stripslashes.html">stripslashes</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.stripos" class="refentry"> <div class="refnamediv"> <h1 class="refname">stripos</h1> <p class="verinfo">(PHP 5, PHP 7)</p><p class="refpurpose"><span class="refname">stripos</span> — <span class="dc-title">Find the position of the first occurrence of a case-insensitive substring in a string</span></p> </div> <div class="refsect1 description" id="refsect1-function.stripos-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">int</span> <span class="methodname"><strong>stripos</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">int</span> <code class="parameter">$offset</code><span class="initializer"> = 0</span></span> ] )</div> <p class="para rdfs-comment"> Find the numeric position of the first occurrence of <code class="parameter">needle</code> in the <code class="parameter">haystack</code> string. </p> <p class="para"> Unlike the <span class="function"><a href="function.strpos.html" class="function">strpos()</a></span>, <span class="function"><strong>stripos()</strong></span> is case-insensitive. </p> </div> <div class="refsect1 parameters" id="refsect1-function.stripos-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <code class="parameter">haystack</code></dt> <dd> <p class="para"> The string to search in. </p> </dd> <dt> <code class="parameter">needle</code></dt> <dd> <p class="para"> Note that the <code class="parameter">needle</code> may be a string of one or more characters. </p> <p class="para"> If <code class="parameter">needle</code> is not a string, it is converted to an integer and applied as the ordinal value of a character. </p> </dd> <dt> <code class="parameter">offset</code></dt> <dd> <p class="para"> If specified, search will start this number of characters counted from the beginning of the string. If the offset is negative, the search will start this number of characters counted from the end of the string. </p> </dd> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-function.stripos-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns the position of where the needle exists relative to the beginnning of the <code class="parameter">haystack</code> string (independent of offset). Also note that string positions start at 0, and not 1. </p> <p class="para"> Returns <strong><code>FALSE</code></strong> if the needle was not found. </p> <div class="warning"><strong class="warning">Warning</strong><p class="simpara">This function may return Boolean <strong><code>FALSE</code></strong>, but may also return a non-Boolean value which evaluates to <strong><code>FALSE</code></strong>. Please read the section on <a href="language.types.boolean.html" class="link">Booleans</a> for more information. Use <a href="language.operators.comparison.html" class="link">the === operator</a> for testing the return value of this function.</p></div> </div> <div class="refsect1 changelog" id="refsect1-function.stripos-changelog"> <h3 class="title">Changelog</h3> <table class="doctable informaltable"> <thead> <tr> <th>Version</th> <th>Description</th> </tr> </thead> <tbody class="tbody"> <tr> <td>7.1.0</td> <td> Support for negative <code class="parameter">offset</code>s has been added. </td> </tr> </tbody> </table> </div> <div class="refsect1 examples" id="refsect1-function.stripos-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-6096"> <p><strong>Example #1 <span class="function"><strong>stripos()</strong></span> examples</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$findme </span><span style="color: #007700">= </span><span style="color: #DD0000">'a'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$mystring1 </span><span style="color: #007700">= </span><span style="color: #DD0000">'xyz'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$mystring2 </span><span style="color: #007700">= </span><span style="color: #DD0000">'ABC'</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$pos1 </span><span style="color: #007700">= </span><span style="color: #0000BB">stripos</span><span style="color: #007700">(</span><span style="color: #0000BB">$mystring1</span><span style="color: #007700">, </span><span style="color: #0000BB">$findme</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$pos2 </span><span style="color: #007700">= </span><span style="color: #0000BB">stripos</span><span style="color: #007700">(</span><span style="color: #0000BB">$mystring2</span><span style="color: #007700">, </span><span style="color: #0000BB">$findme</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// Nope, 'a' is certainly not in 'xyz'<br /></span><span style="color: #007700">if (</span><span style="color: #0000BB">$pos1 </span><span style="color: #007700">=== </span><span style="color: #0000BB">false</span><span style="color: #007700">) {<br /> echo </span><span style="color: #DD0000">"The string '</span><span style="color: #0000BB">$findme</span><span style="color: #DD0000">' was not found in the string '</span><span style="color: #0000BB">$mystring1</span><span style="color: #DD0000">'"</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #FF8000">// Note our use of ===. Simply == would not work as expected<br />// because the position of 'a' is the 0th (first) character.<br /></span><span style="color: #007700">if (</span><span style="color: #0000BB">$pos2 </span><span style="color: #007700">!== </span><span style="color: #0000BB">false</span><span style="color: #007700">) {<br /> echo </span><span style="color: #DD0000">"We found '</span><span style="color: #0000BB">$findme</span><span style="color: #DD0000">' in '</span><span style="color: #0000BB">$mystring2</span><span style="color: #DD0000">' at position </span><span style="color: #0000BB">$pos2</span><span style="color: #DD0000">"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> </div> <div class="refsect1 notes" id="refsect1-function.stripos-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.stripos-seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"><span class="function"><a href="function.mb-stripos.html" class="function" rel="rdfs-seeAlso">mb_stripos()</a> - Finds position of first occurrence of a string within another, case insensitive</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.strrpos.html" class="function" rel="rdfs-seeAlso">strrpos()</a> - Find the position of the last occurrence of a substring in a string</span></li> <li class="member"><span class="function"><a href="function.strripos.html" class="function" rel="rdfs-seeAlso">strripos()</a> - Find the position of the last occurrence of a case-insensitive substring in a string</span></li> <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.substr.html" class="function" rel="rdfs-seeAlso">substr()</a> - Return part of a string</span></li> <li class="member"><span class="function"><a href="function.str-ireplace.html" class="function" rel="rdfs-seeAlso">str_ireplace()</a> - Case-insensitive version of str_replace</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.stripcslashes.html">stripcslashes</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.stripslashes.html">stripslashes</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>