<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <HTML ><HEAD ><TITLE >str_pad</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="str_ireplace" HREF="function.str-ireplace.html"><LINK REL="NEXT" TITLE="str_repeat" HREF="function.str-repeat.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.str-ireplace.html" ACCESSKEY="P" >上一页</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="function.str-repeat.html" ACCESSKEY="N" >下一页</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><H1 ><A NAME="function.str-pad" ></A >str_pad</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN227076" ></A ><P > (PHP 4 >= 4.0.1, PHP 5)</P >str_pad -- Pad a string to a certain length with another string </DIV ><DIV CLASS="refsect1" ><A NAME="AEN227079" ></A ><H2 >Description</H2 >string <B CLASS="methodname" >str_pad</B > ( string input, int pad_length [, string pad_string [, int pad_type]] )<BR ></BR ><P > This functions returns the <CODE CLASS="parameter" >input</CODE > string padded on the left, the right, or both sides to the specified padding length. If the optional argument <CODE CLASS="parameter" >pad_string</CODE > is not supplied, the <CODE CLASS="parameter" >input</CODE > is padded with spaces, otherwise it is padded with characters from <CODE CLASS="parameter" >pad_string</CODE > up to the limit. </P ><P > Optional argument <CODE CLASS="parameter" >pad_type</CODE > can be <TT CLASS="constant" ><B >STR_PAD_RIGHT</B ></TT >, <TT CLASS="constant" ><B >STR_PAD_LEFT</B ></TT >, or <TT CLASS="constant" ><B >STR_PAD_BOTH</B ></TT >. If <CODE CLASS="parameter" >pad_type</CODE > is not specified it is assumed to be <TT CLASS="constant" ><B >STR_PAD_RIGHT</B ></TT >. </P ><P > If the value of <CODE CLASS="parameter" >pad_length</CODE > is negative or less than the length of the input string, no padding takes place. </P ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN227111" ></A ><P ><B >例 1. <B CLASS="function" >str_pad()</B > example</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br />$input </font><font color="#007700">= </font><font color="#DD0000">"Alien"</font><font color="#007700">;<br />echo </font><font color="#0000BB">str_pad</font><font color="#007700">(</font><font color="#0000BB">$input</font><font color="#007700">, </font><font color="#0000BB">10</font><font color="#007700">); </font><font color="#FF8000">// produces "Alien "<br /></font><font color="#007700">echo </font><font color="#0000BB">str_pad</font><font color="#007700">(</font><font color="#0000BB">$input</font><font color="#007700">, </font><font color="#0000BB">10</font><font color="#007700">, </font><font color="#DD0000">"-="</font><font color="#007700">, </font><font color="#0000BB">STR_PAD_LEFT</font><font color="#007700">); </font><font color="#FF8000">// produces "-=-=-Alien"<br /></font><font color="#007700">echo </font><font color="#0000BB">str_pad</font><font color="#007700">(</font><font color="#0000BB">$input</font><font color="#007700">, </font><font color="#0000BB">10</font><font color="#007700">, </font><font color="#DD0000">"_"</font><font color="#007700">, </font><font color="#0000BB">STR_PAD_BOTH</font><font color="#007700">); </font><font color="#FF8000">// produces "__Alien___"<br /></font><font color="#007700">echo </font><font color="#0000BB">str_pad</font><font color="#007700">(</font><font color="#0000BB">$input</font><font color="#007700">, </font><font color="#0000BB">6 </font><font color="#007700">, </font><font color="#DD0000">"___"</font><font color="#007700">); </font><font color="#FF8000">// produces "Alien_"<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >注意: </B > The <CODE CLASS="parameter" >pad_string</CODE > may be truncated if the required number of padding characters can't be evenly divided by the <CODE CLASS="parameter" >pad_string</CODE >'s length. </P ></BLOCKQUOTE ></DIV ></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.str-ireplace.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.str-repeat.html" ACCESSKEY="N" >下一页</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >str_ireplace</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="ref.strings.html" ACCESSKEY="U" >上一级</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >str_repeat</TD ></TR ></TABLE ></DIV ></BODY ></HTML >