<!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>Pad a string to a certain length with another string</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.str-ireplace.html">str_ireplace</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.str-repeat.html">str_repeat</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.str-pad" class="refentry"> <div class="refnamediv"> <h1 class="refname">str_pad</h1> <p class="verinfo">(PHP 4 >= 4.0.1, PHP 5)</p><p class="refpurpose"><span class="refname">str_pad</span> — <span class="dc-title">Pad a string to a certain length with another string</span></p> </div> <div class="refsect1 description" id="refsect1-function.str-pad-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">string</span> <span class="methodname"><strong>str_pad</strong></span> ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$input</code></span> , <span class="methodparam"><span class="type">int</span> <code class="parameter">$pad_length</code></span> [, <span class="methodparam"><span class="type">string</span> <code class="parameter">$pad_string</code><span class="initializer"> = " "</span></span> [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$pad_type</code><span class="initializer"> = STR_PAD_RIGHT</span></span> ]] )</div> <p class="para rdfs-comment"> This functions returns the <em><code class="parameter">input</code></em> string padded on the left, the right, or both sides to the specified padding length. If the optional argument <em><code class="parameter">pad_string</code></em> is not supplied, the <em><code class="parameter">input</code></em> is padded with spaces, otherwise it is padded with characters from <em><code class="parameter">pad_string</code></em> up to the limit. </p> </div> <div class="refsect1 parameters" id="refsect1-function.str-pad-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><em><code class="parameter">input</code></em></span> <dd> <p class="para"> The input string. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">pad_length</code></em></span> <dd> <p class="para"> If the value of <em><code class="parameter">pad_length</code></em> is negative, less than, or equal to the length of the input string, no padding takes place. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">pad_string</code></em></span> <dd> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> The <em><code class="parameter">pad_string</code></em> may be truncated if the required number of padding characters can't be evenly divided by the <em><code class="parameter">pad_string</code></em>'s length. </p> </p></blockquote> </dd> </dt> <dt> <span class="term"><em><code class="parameter">pad_type</code></em></span> <dd> <p class="para"> Optional argument <em><code class="parameter">pad_type</code></em> can be <strong><code>STR_PAD_RIGHT</code></strong>, <strong><code>STR_PAD_LEFT</code></strong>, or <strong><code>STR_PAD_BOTH</code></strong>. If <em><code class="parameter">pad_type</code></em> is not specified it is assumed to be <strong><code>STR_PAD_RIGHT</code></strong>. </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-function.str-pad-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns the padded string. </p> </div> <div class="refsect1 examples" id="refsect1-function.str-pad-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-4876"> <p><strong>Example #1 <span class="function"><strong>str_pad()</strong></span> example</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$input </span><span style="color: #007700">= </span><span style="color: #DD0000">"Alien"</span><span style="color: #007700">;<br />echo </span><span style="color: #0000BB">str_pad</span><span style="color: #007700">(</span><span style="color: #0000BB">$input</span><span style="color: #007700">, </span><span style="color: #0000BB">10</span><span style="color: #007700">); </span><span style="color: #FF8000">// produces "Alien "<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">str_pad</span><span style="color: #007700">(</span><span style="color: #0000BB">$input</span><span style="color: #007700">, </span><span style="color: #0000BB">10</span><span style="color: #007700">, </span><span style="color: #DD0000">"-="</span><span style="color: #007700">, </span><span style="color: #0000BB">STR_PAD_LEFT</span><span style="color: #007700">); </span><span style="color: #FF8000">// produces "-=-=-Alien"<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">str_pad</span><span style="color: #007700">(</span><span style="color: #0000BB">$input</span><span style="color: #007700">, </span><span style="color: #0000BB">10</span><span style="color: #007700">, </span><span style="color: #DD0000">"_"</span><span style="color: #007700">, </span><span style="color: #0000BB">STR_PAD_BOTH</span><span style="color: #007700">); </span><span style="color: #FF8000">// produces "__Alien___"<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">str_pad</span><span style="color: #007700">(</span><span style="color: #0000BB">$input</span><span style="color: #007700">, </span><span style="color: #0000BB">6 </span><span style="color: #007700">, </span><span style="color: #DD0000">"___"</span><span style="color: #007700">); </span><span style="color: #FF8000">// produces "Alien_"<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.str-ireplace.html">str_ireplace</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.str-repeat.html">str_repeat</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>