<HTML ><HEAD ><TITLE >substr</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK REL="HOME" TITLE="PHP Manual" HREF="index.html"><LINK REL="UP" TITLE="String functions" HREF="ref.strings.html"><LINK REL="PREVIOUS" TITLE="substr_replace" HREF="function.substr-replace.html"><LINK REL="NEXT" TITLE="trim" HREF="function.trim.html"><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=ISO-8859-1"></HEAD ><BODY CLASS="refentry" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="3" ALIGN="center" >PHP Manual</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="function.substr-replace.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="function.trim.html" ACCESSKEY="N" >Next</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><H1 ><A NAME="function.substr" ></A >substr</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN91776" ></A ><P > (PHP 3, PHP 4 )</P >substr -- Return part of a string</DIV ><DIV CLASS="refsect1" ><A NAME="AEN91779" ></A ><H2 >Description</H2 >string <B CLASS="methodname" >substr</B > ( string string, int start [, int length])<BR ></BR ><P > <B CLASS="function" >substr()</B > returns the portion of <TT CLASS="parameter" ><I >string</I ></TT > specified by the <TT CLASS="parameter" ><I >start</I ></TT > and <TT CLASS="parameter" ><I >length</I ></TT > parameters. </P ><P > If <TT CLASS="parameter" ><I >start</I ></TT > is non-negative, the returned string will start at the <TT CLASS="parameter" ><I >start</I ></TT >'th position in <TT CLASS="parameter" ><I >string</I ></TT >, counting from zero. For instance, in the string '<TT CLASS="literal" >abcdef</TT >', the character at position <TT CLASS="literal" >0</TT > is '<TT CLASS="literal" >a</TT >', the character at position <TT CLASS="literal" >2</TT > is '<TT CLASS="literal" >c</TT >', and so forth. </P ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN91807" ></A ><P ><B >Example 1. Basic <B CLASS="function" >substr()</B > usage</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $rest = substr("abcdef", 1); // returns "bcdef" $rest = substr("abcdef", 1, 3); // returns "bcd" $rest = substr("abcdef", 0, 4); // returns "abcd" $rest = substr("abcdef", 0, 8); // returns "abcdef" // Accessing via curly braces is another option $string = 'abcdef'; echo $string{0}; // returns a echo $string{3}; // returns d ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ><P > If <TT CLASS="parameter" ><I >start</I ></TT > is negative, the returned string will start at the <TT CLASS="parameter" ><I >start</I ></TT >'th character from the end of <TT CLASS="parameter" ><I >string</I ></TT >. </P ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN91815" ></A ><P ><B >Example 2. Using a negative <TT CLASS="parameter" ><I >start</I ></TT ></B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $rest = substr("abcdef", -1); // returns "f" $rest = substr("abcdef", -2); // returns "ef" $rest = substr("abcdef", -3, 1); // returns "d" ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ><P > If <TT CLASS="parameter" ><I >length</I ></TT > is given and is positive, the string returned will contain at most <TT CLASS="parameter" ><I >length</I ></TT > characters beginning from <TT CLASS="parameter" ><I >start</I ></TT > (depending on the length of <TT CLASS="parameter" ><I >string</I ></TT >). If <TT CLASS="parameter" ><I >string</I ></TT > is less than <TT CLASS="parameter" ><I >start</I ></TT > characters long, <TT CLASS="constant" ><B >FALSE</B ></TT > will be returned. </P ><P > If <TT CLASS="parameter" ><I >length</I ></TT > is given and is negative, then that many characters will be omitted from the end of <TT CLASS="parameter" ><I >string</I ></TT > (after the start position has been calculated when a <TT CLASS="parameter" ><I >start</I ></TT > is negative). If <TT CLASS="parameter" ><I >start</I ></TT > denotes a position beyond this truncation, an empty string will be returned. </P ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN91832" ></A ><P ><B >Example 3. Using a negative <TT CLASS="parameter" ><I >length</I ></TT ></B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $rest = substr("abcdef", 0, -1); // returns "abcde" $rest = substr("abcdef", 2, -1); // returns "cde" $rest = substr("abcdef", 4, -4); // returns "" $rest = substr("abcdef", -3, -1); // returns "de" ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ><P > See also <A HREF="function.strrchr.html" ><B CLASS="function" >strrchr()</B ></A > and <A HREF="function.ereg.html" ><B CLASS="function" >ereg()</B ></A >. </P ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="function.substr-replace.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Home</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="function.trim.html" ACCESSKEY="N" >Next</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >substr_replace</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="ref.strings.html" ACCESSKEY="U" >Up</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >trim</TD ></TR ></TABLE ></DIV ></BODY ></HTML >