Sophie

Sophie

distrib > Mandriva > 2008.1 > x86_64 > by-pkgid > 05cd670d8a02b2b4a0ffb1756f2e8308 > files > 4624

php-manual-zh-5.2.4-1mdv2008.1.noarch.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML
><HEAD
><TITLE
>substr</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK
REL="HOME"
TITLE="PHP 手册"
HREF="index.html"><LINK
REL="UP"
TITLE="String 字符串处理函数"
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=UTF-8"></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 手册</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="function.substr-replace.html"
ACCESSKEY="P"
>上一页</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
></TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="function.trim.html"
ACCESSKEY="N"
>下一页</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><H1
><A
NAME="function.substr"
></A
>substr</H1
><DIV
CLASS="refnamediv"
><A
NAME="AEN228602"
></A
><P
>    (PHP 3, PHP 4, PHP 5)</P
>substr&nbsp;--&nbsp;Return part of a string</DIV
><DIV
CLASS="refsect1"
><A
NAME="AEN228605"
></A
><H2
>Description</H2
>string <B
CLASS="methodname"
>substr</B
> ( string string, int start [, int length] )<BR
></BR
><P
>&#13;     <B
CLASS="function"
>substr()</B
> returns the portion of <CODE
CLASS="parameter"
>string</CODE
>
     specified by the <CODE
CLASS="parameter"
>start</CODE
> and
     <CODE
CLASS="parameter"
>length</CODE
> parameters.
    </P
><P
>&#13;     If <CODE
CLASS="parameter"
>start</CODE
> is non-negative, the returned string
     will start at the <CODE
CLASS="parameter"
>start</CODE
>'th position in
     <CODE
CLASS="parameter"
>string</CODE
>, 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="AEN228633"
></A
><P
><B
>例 1. Basic <B
CLASS="function"
>substr()</B
> usage</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php<br /></font><font color="#007700">echo </font><font color="#0000BB">substr</font><font color="#007700">(</font><font color="#DD0000">'abcdef'</font><font color="#007700">, </font><font color="#0000BB">1</font><font color="#007700">);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#FF8000">// bcdef<br /></font><font color="#007700">echo </font><font color="#0000BB">substr</font><font color="#007700">(</font><font color="#DD0000">'abcdef'</font><font color="#007700">, </font><font color="#0000BB">1</font><font color="#007700">, </font><font color="#0000BB">3</font><font color="#007700">);&nbsp;&nbsp;</font><font color="#FF8000">// bcd<br /></font><font color="#007700">echo </font><font color="#0000BB">substr</font><font color="#007700">(</font><font color="#DD0000">'abcdef'</font><font color="#007700">, </font><font color="#0000BB">0</font><font color="#007700">, </font><font color="#0000BB">4</font><font color="#007700">);&nbsp;&nbsp;</font><font color="#FF8000">// abcd<br /></font><font color="#007700">echo </font><font color="#0000BB">substr</font><font color="#007700">(</font><font color="#DD0000">'abcdef'</font><font color="#007700">, </font><font color="#0000BB">0</font><font color="#007700">, </font><font color="#0000BB">8</font><font color="#007700">);&nbsp;&nbsp;</font><font color="#FF8000">// abcdef<br /></font><font color="#007700">echo </font><font color="#0000BB">substr</font><font color="#007700">(</font><font color="#DD0000">'abcdef'</font><font color="#007700">, -</font><font color="#0000BB">1</font><font color="#007700">, </font><font color="#0000BB">1</font><font color="#007700">); </font><font color="#FF8000">// f<br /><br />// Accessing single characters in a string<br />// can also be achived using "curly braces"<br /></font><font color="#0000BB">$string </font><font color="#007700">= </font><font color="#DD0000">'abcdef'</font><font color="#007700">;<br />echo </font><font color="#0000BB">$string</font><font color="#007700">{</font><font color="#0000BB">0</font><font color="#007700">};&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#FF8000">// a<br /></font><font color="#007700">echo </font><font color="#0000BB">$string</font><font color="#007700">{</font><font color="#0000BB">3</font><font color="#007700">};&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#FF8000">// d<br /></font><font color="#007700">echo </font><font color="#0000BB">$string</font><font color="#007700">{</font><font color="#0000BB">strlen</font><font color="#007700">(</font><font color="#0000BB">$string</font><font color="#007700">)-</font><font color="#0000BB">1</font><font color="#007700">}; </font><font color="#FF8000">// f<br /><br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
><P
>&#13;     If <CODE
CLASS="parameter"
>start</CODE
> is negative, the returned string
     will start at the <CODE
CLASS="parameter"
>start</CODE
>'th character
     from the end of <CODE
CLASS="parameter"
>string</CODE
>.
    </P
><TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN228641"
></A
><P
><B
>例 2. Using a negative <CODE
CLASS="parameter"
>start</CODE
></B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php<br />$rest </font><font color="#007700">= </font><font color="#0000BB">substr</font><font color="#007700">(</font><font color="#DD0000">"abcdef"</font><font color="#007700">, -</font><font color="#0000BB">1</font><font color="#007700">);&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#FF8000">// returns "f"<br /></font><font color="#0000BB">$rest </font><font color="#007700">= </font><font color="#0000BB">substr</font><font color="#007700">(</font><font color="#DD0000">"abcdef"</font><font color="#007700">, -</font><font color="#0000BB">2</font><font color="#007700">);&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#FF8000">// returns "ef"<br /></font><font color="#0000BB">$rest </font><font color="#007700">= </font><font color="#0000BB">substr</font><font color="#007700">(</font><font color="#DD0000">"abcdef"</font><font color="#007700">, -</font><font color="#0000BB">3</font><font color="#007700">, </font><font color="#0000BB">1</font><font color="#007700">); </font><font color="#FF8000">// returns "d"<br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
><P
>&#13;     If <CODE
CLASS="parameter"
>length</CODE
> is given and is positive, the string
     returned will contain at most <CODE
CLASS="parameter"
>length</CODE
> characters
     beginning from <CODE
CLASS="parameter"
>start</CODE
> (depending on the length of
     <CODE
CLASS="parameter"
>string</CODE
>). If <CODE
CLASS="parameter"
>string</CODE
> is less
     than or equal to <CODE
CLASS="parameter"
>start</CODE
> characters long, <TT
CLASS="constant"
><B
>FALSE</B
></TT
>
     will be returned.
    </P
><P
>&#13;     If <CODE
CLASS="parameter"
>length</CODE
> is given and is negative, then that many
     characters will be omitted from the end of <CODE
CLASS="parameter"
>string</CODE
>
     (after the start position has been calculated when a
     <CODE
CLASS="parameter"
>start</CODE
> is negative).  If
     <CODE
CLASS="parameter"
>start</CODE
> 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="AEN228658"
></A
><P
><B
>例 3. Using a negative <CODE
CLASS="parameter"
>length</CODE
></B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php<br />$rest </font><font color="#007700">= </font><font color="#0000BB">substr</font><font color="#007700">(</font><font color="#DD0000">"abcdef"</font><font color="#007700">, </font><font color="#0000BB">0</font><font color="#007700">, -</font><font color="#0000BB">1</font><font color="#007700">);&nbsp;&nbsp;</font><font color="#FF8000">// returns "abcde"<br /></font><font color="#0000BB">$rest </font><font color="#007700">= </font><font color="#0000BB">substr</font><font color="#007700">(</font><font color="#DD0000">"abcdef"</font><font color="#007700">, </font><font color="#0000BB">2</font><font color="#007700">, -</font><font color="#0000BB">1</font><font color="#007700">);&nbsp;&nbsp;</font><font color="#FF8000">// returns "cde"<br /></font><font color="#0000BB">$rest </font><font color="#007700">= </font><font color="#0000BB">substr</font><font color="#007700">(</font><font color="#DD0000">"abcdef"</font><font color="#007700">, </font><font color="#0000BB">4</font><font color="#007700">, -</font><font color="#0000BB">4</font><font color="#007700">);&nbsp;&nbsp;</font><font color="#FF8000">// returns ""<br /></font><font color="#0000BB">$rest </font><font color="#007700">= </font><font color="#0000BB">substr</font><font color="#007700">(</font><font color="#DD0000">"abcdef"</font><font color="#007700">, -</font><font color="#0000BB">3</font><font color="#007700">, -</font><font color="#0000BB">1</font><font color="#007700">); </font><font color="#FF8000">// returns "de"<br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
><P
>&#13;     See also <A
HREF="function.strrchr.html"
><B
CLASS="function"
>strrchr()</B
></A
>,
     <A
HREF="function.substr-replace.html"
><B
CLASS="function"
>substr_replace()</B
></A
>,
     <A
HREF="function.preg-match.html"
><B
CLASS="function"
>preg_match()</B
></A
>,
     <A
HREF="function.trim.html"
><B
CLASS="function"
>trim()</B
></A
>,
     <A
HREF="function.mb-substr.html"
><B
CLASS="function"
>mb_substr()</B
></A
> and
     <A
HREF="function.wordwrap.html"
><B
CLASS="function"
>wordwrap()</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"
>上一页</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>起始页</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="function.trim.html"
ACCESSKEY="N"
>下一页</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"
>上一级</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>trim</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>