<!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 last occurrence of a 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.strripos.html">strripos</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.strspn.html">strspn</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.strrpos" class="refentry"> <div class="refnamediv"> <h1 class="refname">strrpos</h1> <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">strrpos</span> — <span class="dc-title">Find the position of the last occurrence of a substring in a string</span></p> </div> <div class="refsect1 description" id="refsect1-function.strrpos-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">int</span> <span class="methodname"><strong>strrpos</strong></span> ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$haystack</code></span> , <span class="methodparam"><span class="type">string</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 last occurrence of <em><code class="parameter">needle</code></em> in the <em><code class="parameter">haystack</code></em> string. </p> </div> <div class="refsect1 parameters" id="refsect1-function.strrpos-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">offset</code></em></span> <dd> <p class="para"> If specified, search will start this number of characters counted from the beginning of the string. If the value is negative, search will instead start from that many characters from the end of the string, searching backwards. </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-function.strrpos-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns the position where the needle exists relative to the beginnning of the <em><code class="parameter">haystack</code></em> string (independent of search direction or 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.strrpos-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.0.0</td> <td> The <em><code class="parameter">needle</code></em> may now be a string of more than one character. </td> </tr> <tr> <td>5.0.0</td> <td> The <em><code class="parameter">offset</code></em> parameter was introduced. </td> </tr> </tbody> </table> </p> </div> <div class="refsect1 examples" id="refsect1-function.strrpos-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-4901"> <p><strong>Example #1 Checking if a needle is in the haystack</strong></p> <div class="example-contents"><p> It is easy to mistake the return values for "character found at position 0" and "character not found". Here's how to detect the difference: </p></div> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /><br />$pos </span><span style="color: #007700">= </span><span style="color: #0000BB">strrpos</span><span style="color: #007700">(</span><span style="color: #0000BB">$mystring</span><span style="color: #007700">, </span><span style="color: #DD0000">"b"</span><span style="color: #007700">);<br />if (</span><span style="color: #0000BB">$pos </span><span style="color: #007700">=== </span><span style="color: #0000BB">false</span><span style="color: #007700">) { </span><span style="color: #FF8000">// note: three equal signs<br /> // not found...<br /></span><span style="color: #007700">}<br /><br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> <p class="para"> <div class="example" id="example-4902"> <p><strong>Example #2 Searching with offsets</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$foo </span><span style="color: #007700">= </span><span style="color: #DD0000">"0123456789a123456789b123456789c"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">strrpos</span><span style="color: #007700">(</span><span style="color: #0000BB">$foo</span><span style="color: #007700">, </span><span style="color: #DD0000">'7'</span><span style="color: #007700">, -</span><span style="color: #0000BB">5</span><span style="color: #007700">)); </span><span style="color: #FF8000">// Starts looking backwards five positions<br /> // from the end. Result: int(17)<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">strrpos</span><span style="color: #007700">(</span><span style="color: #0000BB">$foo</span><span style="color: #007700">, </span><span style="color: #DD0000">'7'</span><span style="color: #007700">, </span><span style="color: #0000BB">20</span><span style="color: #007700">)); </span><span style="color: #FF8000">// Starts searching 20 positions into the<br /> // string. Result: int(27)<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">strrpos</span><span style="color: #007700">(</span><span style="color: #0000BB">$foo</span><span style="color: #007700">, </span><span style="color: #DD0000">'7'</span><span style="color: #007700">, </span><span style="color: #0000BB">28</span><span style="color: #007700">)); </span><span style="color: #FF8000">// Result: bool(false)<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> </div> <div class="refsect1 seealso" id="refsect1-function.strrpos-seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <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.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.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.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.substr.html" class="function" rel="rdfs-seeAlso">substr()</a> - Return part of a string</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.strripos.html">strripos</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.strspn.html">strspn</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>