<!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>Return part 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.substr-replace.html">substr_replace</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.trim.html">trim</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.substr" class="refentry"> <div class="refnamediv"> <h1 class="refname">substr</h1> <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">substr</span> — <span class="dc-title">Return part of a string</span></p> </div> <div class="refsect1 description" id="refsect1-function.substr-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">string</span> <span class="methodname"><strong>substr</strong></span> ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$string</code></span> , <span class="methodparam"><span class="type">int</span> <code class="parameter">$start</code></span> [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$length</code></span> ] )</div> <p class="para rdfs-comment"> Returns the portion of <em><code class="parameter">string</code></em> specified by the <em><code class="parameter">start</code></em> and <em><code class="parameter">length</code></em> parameters. </p> </div> <div class="refsect1 parameters" id="refsect1-function.substr-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><em><code class="parameter">string</code></em></span> <dd> <p class="para"> The input string. Must be one character or longer. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">start</code></em></span> <dd> <p class="para"> If <em><code class="parameter">start</code></em> is non-negative, the returned string will start at the <em><code class="parameter">start</code></em>'th position in <em><code class="parameter">string</code></em>, counting from zero. For instance, in the string '<em>abcdef</em>', the character at position <em>0</em> is '<em>a</em>', the character at position <em>2</em> is '<em>c</em>', and so forth. </p> <p class="para"> If <em><code class="parameter">start</code></em> is negative, the returned string will start at the <em><code class="parameter">start</code></em>'th character from the end of <em><code class="parameter">string</code></em>. </p> <p class="para"> If <em><code class="parameter">string</code></em> is less than or equal to <em><code class="parameter">start</code></em> characters long, <strong><code>FALSE</code></strong> will be returned. </p> <p class="para"> <div class="example" id="example-4917"> <p><strong>Example #1 Using a negative <em><code class="parameter">start</code></em></strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$rest </span><span style="color: #007700">= </span><span style="color: #0000BB">substr</span><span style="color: #007700">(</span><span style="color: #DD0000">"abcdef"</span><span style="color: #007700">, -</span><span style="color: #0000BB">1</span><span style="color: #007700">); </span><span style="color: #FF8000">// returns "f"<br /></span><span style="color: #0000BB">$rest </span><span style="color: #007700">= </span><span style="color: #0000BB">substr</span><span style="color: #007700">(</span><span style="color: #DD0000">"abcdef"</span><span style="color: #007700">, -</span><span style="color: #0000BB">2</span><span style="color: #007700">); </span><span style="color: #FF8000">// returns "ef"<br /></span><span style="color: #0000BB">$rest </span><span style="color: #007700">= </span><span style="color: #0000BB">substr</span><span style="color: #007700">(</span><span style="color: #DD0000">"abcdef"</span><span style="color: #007700">, -</span><span style="color: #0000BB">3</span><span style="color: #007700">, </span><span style="color: #0000BB">1</span><span style="color: #007700">); </span><span style="color: #FF8000">// returns "d"<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">length</code></em></span> <dd> <p class="para"> If <em><code class="parameter">length</code></em> is given and is positive, the string returned will contain at most <em><code class="parameter">length</code></em> characters beginning from <em><code class="parameter">start</code></em> (depending on the length of <em><code class="parameter">string</code></em>). </p> <p class="para"> If <em><code class="parameter">length</code></em> is given and is negative, then that many characters will be omitted from the end of <em><code class="parameter">string</code></em> (after the start position has been calculated when a <em><code class="parameter">start</code></em> is negative). If <em><code class="parameter">start</code></em> denotes the position of this truncation or beyond, false will be returned. </p> <p class="para"> If <em><code class="parameter">length</code></em> is given and is <em>0</em>, <strong><code>FALSE</code></strong> or <strong><code>NULL</code></strong> an empty string will be returned. </p> <p class="para"> If <em><code class="parameter">length</code></em> is omitted, the substring starting from <em><code class="parameter">start</code></em> until the end of the string will be returned. </p> <div class="example" id="example-4918"> <p><strong>Example #2 Using a negative <em><code class="parameter">length</code></em></strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$rest </span><span style="color: #007700">= </span><span style="color: #0000BB">substr</span><span style="color: #007700">(</span><span style="color: #DD0000">"abcdef"</span><span style="color: #007700">, </span><span style="color: #0000BB">0</span><span style="color: #007700">, -</span><span style="color: #0000BB">1</span><span style="color: #007700">); </span><span style="color: #FF8000">// returns "abcde"<br /></span><span style="color: #0000BB">$rest </span><span style="color: #007700">= </span><span style="color: #0000BB">substr</span><span style="color: #007700">(</span><span style="color: #DD0000">"abcdef"</span><span style="color: #007700">, </span><span style="color: #0000BB">2</span><span style="color: #007700">, -</span><span style="color: #0000BB">1</span><span style="color: #007700">); </span><span style="color: #FF8000">// returns "cde"<br /></span><span style="color: #0000BB">$rest </span><span style="color: #007700">= </span><span style="color: #0000BB">substr</span><span style="color: #007700">(</span><span style="color: #DD0000">"abcdef"</span><span style="color: #007700">, </span><span style="color: #0000BB">4</span><span style="color: #007700">, -</span><span style="color: #0000BB">4</span><span style="color: #007700">); </span><span style="color: #FF8000">// returns false<br /></span><span style="color: #0000BB">$rest </span><span style="color: #007700">= </span><span style="color: #0000BB">substr</span><span style="color: #007700">(</span><span style="color: #DD0000">"abcdef"</span><span style="color: #007700">, -</span><span style="color: #0000BB">3</span><span style="color: #007700">, -</span><span style="color: #0000BB">1</span><span style="color: #007700">); </span><span style="color: #FF8000">// returns "de"<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-function.substr-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns the extracted part of string; or <strong><code>FALSE</code></strong> on failure, or an empty <em><code class="parameter">string</code></em>. </p> </div> <div class="refsect1 changelog" id="refsect1-function.substr-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.2.2 - 5.2.6</td> <td> If the <em><code class="parameter">start</code></em> parameter indicates the position of a negative truncation or beyond, false is returned. Other versions get the string from start. </td> </tr> </tbody> </table> </p> </div> <div class="refsect1 examples" id="refsect1-function.substr-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-4919"> <p><strong>Example #3 Basic <span class="function"><strong>substr()</strong></span> usage</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">substr</span><span style="color: #007700">(</span><span style="color: #DD0000">'abcdef'</span><span style="color: #007700">, </span><span style="color: #0000BB">1</span><span style="color: #007700">); </span><span style="color: #FF8000">// bcdef<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">substr</span><span style="color: #007700">(</span><span style="color: #DD0000">'abcdef'</span><span style="color: #007700">, </span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">3</span><span style="color: #007700">); </span><span style="color: #FF8000">// bcd<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">substr</span><span style="color: #007700">(</span><span style="color: #DD0000">'abcdef'</span><span style="color: #007700">, </span><span style="color: #0000BB">0</span><span style="color: #007700">, </span><span style="color: #0000BB">4</span><span style="color: #007700">); </span><span style="color: #FF8000">// abcd<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">substr</span><span style="color: #007700">(</span><span style="color: #DD0000">'abcdef'</span><span style="color: #007700">, </span><span style="color: #0000BB">0</span><span style="color: #007700">, </span><span style="color: #0000BB">8</span><span style="color: #007700">); </span><span style="color: #FF8000">// abcdef<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">substr</span><span style="color: #007700">(</span><span style="color: #DD0000">'abcdef'</span><span style="color: #007700">, -</span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">1</span><span style="color: #007700">); </span><span style="color: #FF8000">// f<br /><br />// Accessing single characters in a string<br />// can also be achieved using "square brackets"<br /></span><span style="color: #0000BB">$string </span><span style="color: #007700">= </span><span style="color: #DD0000">'abcdef'</span><span style="color: #007700">;<br />echo </span><span style="color: #0000BB">$string</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">]; </span><span style="color: #FF8000">// a<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">$string</span><span style="color: #007700">[</span><span style="color: #0000BB">3</span><span style="color: #007700">]; </span><span style="color: #FF8000">// d<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">$string</span><span style="color: #007700">[</span><span style="color: #0000BB">strlen</span><span style="color: #007700">(</span><span style="color: #0000BB">$string</span><span style="color: #007700">)-</span><span style="color: #0000BB">1</span><span style="color: #007700">]; </span><span style="color: #FF8000">// f<br /><br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> <div class="example" id="example-4920"> <p><strong>Example #4 <span class="function"><strong>substr()</strong></span> casting behaviour</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #007700">class </span><span style="color: #0000BB">apple </span><span style="color: #007700">{<br /> public function </span><span style="color: #0000BB">__toString</span><span style="color: #007700">() {<br /> return </span><span style="color: #DD0000">"green"</span><span style="color: #007700">;<br /> }<br />}<br /><br />echo </span><span style="color: #DD0000">"1) "</span><span style="color: #007700">.</span><span style="color: #0000BB">var_export</span><span style="color: #007700">(</span><span style="color: #0000BB">substr</span><span style="color: #007700">(</span><span style="color: #DD0000">"pear"</span><span style="color: #007700">, </span><span style="color: #0000BB">0</span><span style="color: #007700">, </span><span style="color: #0000BB">2</span><span style="color: #007700">), </span><span style="color: #0000BB">true</span><span style="color: #007700">).</span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br />echo </span><span style="color: #DD0000">"2) "</span><span style="color: #007700">.</span><span style="color: #0000BB">var_export</span><span style="color: #007700">(</span><span style="color: #0000BB">substr</span><span style="color: #007700">(</span><span style="color: #0000BB">54321</span><span style="color: #007700">, </span><span style="color: #0000BB">0</span><span style="color: #007700">, </span><span style="color: #0000BB">2</span><span style="color: #007700">), </span><span style="color: #0000BB">true</span><span style="color: #007700">).</span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br />echo </span><span style="color: #DD0000">"3) "</span><span style="color: #007700">.</span><span style="color: #0000BB">var_export</span><span style="color: #007700">(</span><span style="color: #0000BB">substr</span><span style="color: #007700">(new </span><span style="color: #0000BB">apple</span><span style="color: #007700">(), </span><span style="color: #0000BB">0</span><span style="color: #007700">, </span><span style="color: #0000BB">2</span><span style="color: #007700">), </span><span style="color: #0000BB">true</span><span style="color: #007700">).</span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br />echo </span><span style="color: #DD0000">"4) "</span><span style="color: #007700">.</span><span style="color: #0000BB">var_export</span><span style="color: #007700">(</span><span style="color: #0000BB">substr</span><span style="color: #007700">(</span><span style="color: #0000BB">true</span><span style="color: #007700">, </span><span style="color: #0000BB">0</span><span style="color: #007700">, </span><span style="color: #0000BB">1</span><span style="color: #007700">), </span><span style="color: #0000BB">true</span><span style="color: #007700">).</span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br />echo </span><span style="color: #DD0000">"5) "</span><span style="color: #007700">.</span><span style="color: #0000BB">var_export</span><span style="color: #007700">(</span><span style="color: #0000BB">substr</span><span style="color: #007700">(</span><span style="color: #0000BB">false</span><span style="color: #007700">, </span><span style="color: #0000BB">0</span><span style="color: #007700">, </span><span style="color: #0000BB">1</span><span style="color: #007700">), </span><span style="color: #0000BB">true</span><span style="color: #007700">).</span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br />echo </span><span style="color: #DD0000">"6) "</span><span style="color: #007700">.</span><span style="color: #0000BB">var_export</span><span style="color: #007700">(</span><span style="color: #0000BB">substr</span><span style="color: #007700">(</span><span style="color: #DD0000">""</span><span style="color: #007700">, </span><span style="color: #0000BB">0</span><span style="color: #007700">, </span><span style="color: #0000BB">1</span><span style="color: #007700">), </span><span style="color: #0000BB">true</span><span style="color: #007700">).</span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br />echo </span><span style="color: #DD0000">"7) "</span><span style="color: #007700">.</span><span style="color: #0000BB">var_export</span><span style="color: #007700">(</span><span style="color: #0000BB">substr</span><span style="color: #007700">(</span><span style="color: #0000BB">1.2e3</span><span style="color: #007700">, </span><span style="color: #0000BB">0</span><span style="color: #007700">, </span><span style="color: #0000BB">4</span><span style="color: #007700">), </span><span style="color: #0000BB">true</span><span style="color: #007700">).</span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"><p>The above example will output:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> 1) 'pe' 2) '54' 3) 'gr' 4) '1' 5) false 6) false 7) '1200' </pre></div> </div> </div> </p> </div> <div class="refsect1 errors" id="refsect1-function.substr-errors"> <h3 class="title">Errors/Exceptions</h3> <p class="para"> Returns <strong><code>FALSE</code></strong> on error. <div class="example" id="example-4921"> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">substr</span><span style="color: #007700">(</span><span style="color: #DD0000">'a'</span><span style="color: #007700">, </span><span style="color: #0000BB">1</span><span style="color: #007700">)); </span><span style="color: #FF8000">// bool(false)<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> </div> <div class="refsect1 seealso" id="refsect1-function.substr-seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <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-replace.html" class="function" rel="rdfs-seeAlso">substr_replace()</a> - Replace text within a portion of a string</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> <li class="member"> <span class="function"><a href="function.trim.html" class="function" rel="rdfs-seeAlso">trim()</a> - Strip whitespace (or other characters) from the beginning and end of a string</span></li> <li class="member"> <span class="function"><a href="function.mb-substr.html" class="function" rel="rdfs-seeAlso">mb_substr()</a> - Get part of string</span></li> <li class="member"> <span class="function"><a href="function.wordwrap.html" class="function" rel="rdfs-seeAlso">wordwrap()</a> - Wraps a string to a given number of characters</span></li> <li class="member"><a href="language.types.string.html#language.types.string.substr" class="link">String access and modification by character</a></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.substr-replace.html">substr_replace</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.trim.html">trim</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>