<!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 last occurrence of a character 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.strpos.html">strpos</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.strrev.html">strrev</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.strrchr" class="refentry"> <div class="refnamediv"> <h1 class="refname">strrchr</h1> <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">strrchr</span> — <span class="dc-title">Find the last occurrence of a character in a string</span></p> </div> <div class="refsect1 description" id="refsect1-function.strrchr-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">string</span> <span class="methodname"><strong>strrchr</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> )</div> <p class="para rdfs-comment"> This function returns the portion of <em><code class="parameter">haystack</code></em> which starts at the last occurrence of <em><code class="parameter">needle</code></em> and goes until the end of <em><code class="parameter">haystack</code></em>. </p> </div> <div class="refsect1 parameters" id="refsect1-function.strrchr-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> contains more than one character, only the first is used. This behavior is different from that of <span class="function"><a href="function.strstr.html" class="function">strstr()</a></span>. </p> <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> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-function.strrchr-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> This function 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.strrchr-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>4.3.0</td> <td> This function is now binary safe. </td> </tr> </tbody> </table> </p> </div> <div class="refsect1 examples" id="refsect1-function.strrchr-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-4898"> <p><strong>Example #1 <span class="function"><strong>strrchr()</strong></span> example</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #FF8000">// get last directory in $PATH<br /></span><span style="color: #0000BB">$dir </span><span style="color: #007700">= </span><span style="color: #0000BB">substr</span><span style="color: #007700">(</span><span style="color: #0000BB">strrchr</span><span style="color: #007700">(</span><span style="color: #0000BB">$PATH</span><span style="color: #007700">, </span><span style="color: #DD0000">":"</span><span style="color: #007700">), </span><span style="color: #0000BB">1</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// get everything after last newline<br /></span><span style="color: #0000BB">$text </span><span style="color: #007700">= </span><span style="color: #DD0000">"Line 1\nLine 2\nLine 3"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$last </span><span style="color: #007700">= </span><span style="color: #0000BB">substr</span><span style="color: #007700">(</span><span style="color: #0000BB">strrchr</span><span style="color: #007700">(</span><span style="color: #0000BB">$text</span><span style="color: #007700">, </span><span style="color: #0000BB">10</span><span style="color: #007700">), </span><span style="color: #0000BB">1 </span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> </div> <div class="refsect1 notes" id="refsect1-function.strrchr-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.strrchr-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.strrpos.html" class="function" rel="rdfs-seeAlso">strrpos()</a> - Find the position of the last occurrence of a substring in 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.strpos.html">strpos</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.strrev.html">strrev</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>