xref: /web-php/manual/en/function.rtrim.php (revision 50202bbf)
1<?php
2include_once __DIR__ . '/../../include/shared-manual.inc';
3$TOC = array();
4$PARENTS = array();
5include_once __DIR__ ."/toc/ref.strings.inc";
6$setup = array (
7  'home' =>
8  array (
9    0 => 'index.php',
10    1 => 'PHP Manual',
11  ),
12  'head' =>
13  array (
14    0 => 'UTF-8',
15    1 => 'en',
16  ),
17  'this' =>
18  array (
19    0 => 'function.rtrim.php',
20    1 => 'rtrim',
21  ),
22  'up' =>
23  array (
24    0 => 'ref.strings.php',
25    1 => 'String Functions',
26  ),
27  'prev' =>
28  array (
29    0 => 'function.strpos.php',
30    1 => 'strpos',
31  ),
32  'alternatives' =>
33  array (
34  ),
35);
36$setup["toc"] = $TOC;
37$setup["parents"] = $PARENTS;
38manual_setup($setup);
39
40?>
41<div id="function.rtrim" class="refentry">
42    <div class="refnamediv">
43        <h1 class="refname">rtrim</h1>
44        <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">rtrim</span> &mdash; <span class="dc-title">Strip whitespace (or other characters) from the end of a string</span></p>
45
46    </div>
47
48    <div class="refsect1 description" id="refsect1-function.rtrim-description">
49        <h3 class="title">Description</h3>
50        <div class="methodsynopsis dc-description">
51            <span class="methodname"><strong>rtrim</strong></span>(<span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$string</code></span>, <span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$characters</code><span class="initializer"> = &quot; \n\r\t\v\x00&quot;</span></span>): <span class="type"><a href="language.types.string.php" class="type string">string</a></span></div>
52
53        <p class="simpara">
54            This function returns a string with whitespace (or other characters) stripped from the
55            end of <code class="parameter">string</code>.
56        </p>
57        <p class="simpara">
58            Without the second parameter,
59            <span class="function"><strong>rtrim()</strong></span> will strip these characters:
60        </p>
61
62        <ul class="itemizedlist">
63            <li class="listitem">
64     <span class="simpara">
65      <code class="literal">&quot; &quot;</code>: <abbr title="American Standard Code for Information Interchange">ASCII</abbr> <abbr>SP</abbr> character
66      <code class="literal">0x20</code>, an ordinary space.
67     </span>
68            </li>
69            <li class="listitem">
70     <span class="simpara">
71      <code class="literal">&quot;\t&quot;</code>: <abbr title="American Standard Code for Information Interchange">ASCII</abbr> <abbr>HT</abbr> character
72      <code class="literal">0x09</code>, a tab.
73     </span>
74            </li>
75            <li class="listitem">
76     <span class="simpara">
77      <code class="literal">&quot;\n&quot;</code>: <abbr title="American Standard Code for Information Interchange">ASCII</abbr> <abbr>LF</abbr> character
78      <code class="literal">0x0A</code>, a new line (line feed).
79     </span>
80            </li>
81            <li class="listitem">
82     <span class="simpara">
83      <code class="literal">&quot;\r&quot;</code>: <abbr title="American Standard Code for Information Interchange">ASCII</abbr> <abbr>CR</abbr> character
84      <code class="literal">0x0D</code>, a carriage return.
85     </span>
86            </li>
87            <li class="listitem">
88     <span class="simpara">
89      <code class="literal">&quot;\0&quot;</code>: <abbr title="American Standard Code for Information Interchange">ASCII</abbr> <abbr>NUL</abbr> character
90      <code class="literal">0x00</code>, the NUL-byte.
91     </span>
92            </li>
93            <li class="listitem">
94     <span class="simpara">
95      <code class="literal">&quot;\v&quot;</code>: <abbr title="American Standard Code for Information Interchange">ASCII</abbr> <abbr>VT</abbr>
96      character <code class="literal">0x0B</code>, a vertical tab.
97     </span>
98            </li>
99        </ul>
100
101    </div>
102
103
104    <div class="refsect1 parameters" id="refsect1-function.rtrim-parameters">
105        <h3 class="title">Parameters</h3>
106        <dl>
107
108            <dt><code class="parameter">string</code></dt>
109            <dd>
110     <span class="simpara">
111      The input string.
112     </span>
113            </dd>
114
115
116            <dt><code class="parameter">characters</code></dt>
117            <dd>
118
119 <span class="simpara">
120  Optionally, the stripped characters can also be specified using
121  the <code class="parameter">characters</code> parameter.
122  Simply list all characters that need to be stripped.
123  With <code class="literal">..</code> it is possible to specify an incrementing range of characters.
124 </span>
125
126            </dd>
127
128        </dl>
129    </div>
130
131
132    <div class="refsect1 returnvalues" id="refsect1-function.rtrim-returnvalues">
133        <h3 class="title">Return Values</h3>
134        <p class="simpara">
135            Returns the modified string.
136        </p>
137    </div>
138
139
140    <div class="refsect1 examples" id="refsect1-function.rtrim-examples">
141        <h3 class="title">Examples</h3>
142        <div class="example" id="example-4971">
143            <p><strong>Example #1 Usage example of <span class="function"><strong>rtrim()</strong></span></strong></p>
144            <div class="example-contents">
145                <div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /><br />$text </span><span style="color: #007700">= </span><span style="color: #DD0000">"\t\tThese are a few words :) ...  "</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$binary </span><span style="color: #007700">= </span><span style="color: #DD0000">"\x09Example string\x0A"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$hello  </span><span style="color: #007700">= </span><span style="color: #DD0000">"Hello 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">, </span><span style="color: #0000BB">$binary</span><span style="color: #007700">, </span><span style="color: #0000BB">$hello</span><span style="color: #007700">);<br /><br />print </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$trimmed </span><span style="color: #007700">= </span><span style="color: #0000BB">rtrim</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 </span><span style="color: #007700">= </span><span style="color: #0000BB">rtrim</span><span style="color: #007700">(</span><span style="color: #0000BB">$text</span><span style="color: #007700">, </span><span style="color: #DD0000">" \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 </span><span style="color: #007700">= </span><span style="color: #0000BB">rtrim</span><span style="color: #007700">(</span><span style="color: #0000BB">$hello</span><span style="color: #007700">, </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: #FF8000">// trim the ASCII control characters at the end of $binary<br />// (from 0 to 31 inclusive)<br /></span><span style="color: #0000BB">$clean </span><span style="color: #007700">= </span><span style="color: #0000BB">rtrim</span><span style="color: #007700">(</span><span style="color: #0000BB">$binary</span><span style="color: #007700">, </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>
146            </div>
147
148            <div class="example-contents"><p>The above example will output:</p></div>
149            <div class="example-contents screen">
150                <div class="examplescode"><pre class="examplescode">string(32) &quot;        These are a few words :) ...  &quot;
151string(16) &quot;    Example string
152&quot;
153string(11) &quot;Hello World&quot;
154
155string(30) &quot;        These are a few words :) ...&quot;
156string(26) &quot;        These are a few words :)&quot;
157string(9) &quot;Hello Wor&quot;
158string(15) &quot;    Example string&quot;</pre>
159                </div>
160            </div>
161        </div>
162        <div class="example" id="similar_text.example.swapping">
163            <p><strong>Example #1 <span class="function"><strong>similar_text()</strong></span> argument swapping example</strong></p>
164            <div class="example-contents"><p>
165                    This example shows that swapping the <code class="parameter">string1</code> and
166                    <code class="parameter">string2</code> argument may yield different results.
167                </p></div>
168            <div class="example-contents">
169                <div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$sim </span><span style="color: #007700">= </span><span style="color: #0000BB">similar_text</span><span style="color: #007700">(</span><span style="color: #DD0000">'bafoobar'</span><span style="color: #007700">, </span><span style="color: #DD0000">'barfoo'</span><span style="color: #007700">, </span><span style="color: #0000BB">$perc</span><span style="color: #007700">);<br />echo </span><span style="color: #DD0000">"similarity: </span><span style="color: #0000BB">$sim</span><span style="color: #DD0000"> (</span><span style="color: #0000BB">$perc</span><span style="color: #DD0000"> %)\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$sim </span><span style="color: #007700">= </span><span style="color: #0000BB">similar_text</span><span style="color: #007700">(</span><span style="color: #DD0000">'barfoo'</span><span style="color: #007700">, </span><span style="color: #DD0000">'bafoobar'</span><span style="color: #007700">, </span><span style="color: #0000BB">$perc</span><span style="color: #007700">);<br />echo </span><span style="color: #DD0000">"similarity: </span><span style="color: #0000BB">$sim</span><span style="color: #DD0000"> (</span><span style="color: #0000BB">$perc</span><span style="color: #DD0000"> %)\n"</span><span style="color: #007700">;</span></span></code></div>
170            </div>
171
172            <div class="example-contents"><p>The above example will output
173                    something similar to:</p></div>
174            <div class="example-contents screen">
175                <div class="examplescode"><pre class="examplescode">similarity: 5 (71.428571428571 %)
176similarity: 3 (42.857142857143 %)</pre>
177                </div>
178            </div>
179        </div>
180    </div>
181
182
183    <div class="refsect1 seealso" id="refsect1-function.rtrim-seealso">
184        <h3 class="title">See Also</h3>
185        <ul class="simplelist">
186            <li><span class="function"><a href="function.trim.php" class="function" rel="rdfs-seeAlso">trim()</a> - Strip whitespace (or other characters) from the beginning and end of a string</span></li>
187            <li><span class="function"><a href="function.ltrim.php" class="function" rel="rdfs-seeAlso">ltrim()</a> - Strip whitespace (or other characters) from the beginning of a string</span></li>
188        </ul>
189    </div>
190</div><?php manual_footer([]); ?>
191