Sophie

Sophie

distrib > Mageia > 4 > x86_64 > by-pkgid > f800694edefe91adea2624f711a41a2d > files > 6452

php-manual-en-5.5.7-1.mga4.noarch.rpm

<!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>Strip whitespace (or other characters) from the beginning and end 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.html">substr</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.ucfirst.html">ucfirst</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.trim" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">trim</h1> 
  <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">trim</span> &mdash; <span class="dc-title">Strip whitespace (or other characters) from the beginning and end of a string</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.trim-description">
  <h3 class="title">Description</h3>
  <div class="methodsynopsis dc-description">
   <span class="type">string</span> <span class="methodname"><strong>trim</strong></span>
    ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$str</code></span>
   [, <span class="methodparam"><span class="type">string</span> <code class="parameter">$charlist</code><span class="initializer"> = &quot; \t\n\r\0\x0B&quot;</span></span>
  ] )</div>

  <p class="para rdfs-comment">
   This function returns a string with whitespace stripped from the
   beginning and end of <em><code class="parameter">str</code></em>.
   Without the second parameter,
    <span class="function"><strong>trim()</strong></span> will strip these characters:
   
   <ul class="itemizedlist">
    <li class="listitem">
     <span class="simpara">
      &quot; &quot; (<acronym title="American Standard Code for Information Interchange">ASCII</acronym> <em>32</em> 
      (<em>0x20</em>)), an ordinary space.
     </span>
    </li>
    <li class="listitem">
     <span class="simpara">
      &quot;\t&quot; (<acronym title="American Standard Code for Information Interchange">ASCII</acronym> <em>9</em> 
      (<em>0x09</em>)), a tab.
     </span>
    </li>
    <li class="listitem">
     <span class="simpara">
      &quot;\n&quot; (<acronym title="American Standard Code for Information Interchange">ASCII</acronym> <em>10</em> 
      (<em>0x0A</em>)), a new line (line feed).
     </span>
    </li>
    <li class="listitem">
     <span class="simpara">
      &quot;\r&quot; (<acronym title="American Standard Code for Information Interchange">ASCII</acronym> <em>13</em> 
      (<em>0x0D</em>)), a carriage return.
     </span>
    </li>
    <li class="listitem">
     <span class="simpara">
      &quot;\0&quot; (<acronym title="American Standard Code for Information Interchange">ASCII</acronym> <em>0</em> 
      (<em>0x00</em>)), the <em>NUL</em>-byte.
     </span>
    </li>
    <li class="listitem">
     <span class="simpara"> 
      &quot;\x0B&quot; (<acronym title="American Standard Code for Information Interchange">ASCII</acronym> <em>11</em> 
      (<em>0x0B</em>)), a vertical tab.
     </span>
    </li>
   </ul>
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.trim-parameters">
  <h3 class="title">Parameters</h3>
  <p class="para">
   <dl>

    <dt>

     <span class="term"><em><code class="parameter">str</code></em></span>
     <dd>

      <p class="para">
       The <span class="type"><a href="language.types.string.html" class="type string">string</a></span> that will be trimmed.
      </p>
     </dd>

    </dt>

    <dt>

     <span class="term"><em><code class="parameter">charlist</code></em></span>
     <dd>

      <p class="para">
       Optionally, the stripped characters can also be specified using
       the <em><code class="parameter">charlist</code></em> parameter.
       Simply list all characters that you want to be stripped. With
       <em>..</em> you can specify a range of characters.
      </p>
     </dd>

    </dt>

   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.trim-returnvalues">
  <h3 class="title">Return Values</h3>
  <p class="para">
   The trimmed string.
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.trim-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>4.1.0</td>
       <td>
        The optional <em><code class="parameter">charlist</code></em> parameter was added.
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.trim-examples">
  <h3 class="title">Examples</h3>
  <p class="para">
   <div class="example" id="example-4922">
    <p><strong>Example #1 Usage example of  <span class="function"><strong>trim()</strong></span></strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />$text&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"\t\tThese&nbsp;are&nbsp;a&nbsp;few&nbsp;words&nbsp;:)&nbsp;...&nbsp;&nbsp;"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$binary&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"\x09Example&nbsp;string\x0A"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$hello&nbsp;&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"Hello&nbsp;World"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$text</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$binary</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$hello</span><span style="color: #007700">);<br /><br />print&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$trimmed&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">trim</span><span style="color: #007700">(</span><span style="color: #0000BB">$text</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$trimmed</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$trimmed&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">trim</span><span style="color: #007700">(</span><span style="color: #0000BB">$text</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"&nbsp;\t."</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$trimmed</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$trimmed&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">trim</span><span style="color: #007700">(</span><span style="color: #0000BB">$hello</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"Hdle"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$trimmed</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$trimmed&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">trim</span><span style="color: #007700">(</span><span style="color: #0000BB">$hello</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'HdWr'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$trimmed</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;trim&nbsp;the&nbsp;ASCII&nbsp;control&nbsp;characters&nbsp;at&nbsp;the&nbsp;beginning&nbsp;and&nbsp;end&nbsp;of&nbsp;$binary<br />//&nbsp;(from&nbsp;0&nbsp;to&nbsp;31&nbsp;inclusive)<br /></span><span style="color: #0000BB">$clean&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">trim</span><span style="color: #007700">(</span><span style="color: #0000BB">$binary</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"\x00..\x1F"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$clean</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?&gt;</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>
string(32) &quot;        These are a few words :) ...  &quot;
string(16) &quot;    Example string
&quot;
string(11) &quot;Hello World&quot;

string(28) &quot;These are a few words :) ...&quot;
string(24) &quot;These are a few words :)&quot;
string(5) &quot;o Wor&quot;
string(9) &quot;ello Worl&quot;
string(14) &quot;Example string&quot;
</pre></div>
    </div>
   </div>
  </p>
  <p class="para">
   <div class="example" id="example-4923">
    <p><strong>Example #2 Trimming array values with  <span class="function"><strong>trim()</strong></span></strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">trim_value</span><span style="color: #007700">(&amp;</span><span style="color: #0000BB">$value</span><span style="color: #007700">)&nbsp;<br />{&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$value&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">trim</span><span style="color: #007700">(</span><span style="color: #0000BB">$value</span><span style="color: #007700">);&nbsp;<br />}<br /><br /></span><span style="color: #0000BB">$fruit&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">'apple'</span><span style="color: #007700">,</span><span style="color: #DD0000">'banana&nbsp;'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'&nbsp;cranberry&nbsp;'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$fruit</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">array_walk</span><span style="color: #007700">(</span><span style="color: #0000BB">$fruit</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'trim_value'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$fruit</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?&gt;</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>
array(3) {
  [0]=&gt;
  string(5) &quot;apple&quot;
  [1]=&gt;
  string(7) &quot;banana &quot;
  [2]=&gt;
  string(11) &quot; cranberry &quot;
}
array(3) {
  [0]=&gt;
  string(5) &quot;apple&quot;
  [1]=&gt;
  string(6) &quot;banana&quot;
  [2]=&gt;
  string(9) &quot;cranberry&quot;
}

</pre></div>
    </div>
   </div>
  </p>
 </div>


 <div class="refsect1 notes" id="refsect1-function.trim-notes">
  <h3 class="title">Notes</h3>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <strong>Possible gotcha: removing middle characters</strong><br />
   <p class="para">
    Because  <span class="function"><strong>trim()</strong></span> trims characters from the beginning and end of
    a <span class="type"><a href="language.types.string.html" class="type string">string</a></span>, it may be confusing when characters are (or are not) removed from
    the middle. <em>trim(&#039;abc&#039;, &#039;bad&#039;)</em> removes both &#039;a&#039; and &#039;b&#039; because it
    trims &#039;a&#039; thus moving &#039;b&#039; to the beginning to also be trimmed. So, this is why it &quot;works&quot;
    whereas <em>trim(&#039;abc&#039;, &#039;b&#039;)</em> seemingly does not.
   </p>
  </p></blockquote>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.trim-seealso">
  <h3 class="title">See Also</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"> <span class="function"><a href="function.ltrim.html" class="function" rel="rdfs-seeAlso">ltrim()</a> - Strip whitespace (or other characters) from the beginning of a string</span></li>
    <li class="member"> <span class="function"><a href="function.rtrim.html" class="function" rel="rdfs-seeAlso">rtrim()</a> - Strip whitespace (or other characters) from the end of a string</span></li>
    <li class="member"> <span class="function"><a href="function.str-replace.html" class="function" rel="rdfs-seeAlso">str_replace()</a> - Replace all occurrences of the search string with the replacement string</span></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.html">substr</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.ucfirst.html">ucfirst</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>