|
|
 |
rtrim (PHP 3, PHP 4, PHP 5) rtrim --
Strip whitespace (or other characters) from the end of a string
Descriptionstring rtrim ( string str [, string charlist] ) Note:
The second parameter was added in PHP 4.1.0
This function returns a string with whitespace stripped from the
end of str.
Without the second parameter,
rtrim() will strip these characters:
" " (ASCII 32
(0x20)), an ordinary space.
"\t" (ASCII 9
(0x09)), a tab.
"\n" (ASCII 10
(0x0A)), a new line (line feed).
"\r" (ASCII 13
(0x0D)), a carriage return.
"\0" (ASCII 0
(0x00)), the NUL-byte.
"\x0B" (ASCII 11
(0x0B)), a vertical tab.
You can also specify the characters you want to strip, by means
of the charlist parameter.
Simply list all characters that you want to be stripped. With
.. you can specify a range of characters.
Example 1. Usage example of rtrim() |
<?php
$text = "\t\tThese are a few words :) ... ";
$trimmed = rtrim($text);
$trimmed = rtrim($text, " \t.");
$clean = rtrim($binary, "\x00..\x1F");
?>
|
|
See also trim() and ltrim().
User Contributed Notes
rtrim
Unimagined at UnaimaginedDesigns dot Com
16-Jan-2005 02:49
I needed a way to trim all white space and then a few chosen strings from the end of a string. So I wrote this class to reuse when stuff needs to be trimmed.
<?php
class cleaner {
function cleaner ($cuts,$pinfo) {
$ucut = "0";
$lcut = "0";
while ($cuts[$ucut]) {
$lcut++;
$ucut++;
}
$lcut = $lcut - 1;
$ucut = "0";
$rcut = "0";
$wiy = "start";
while ($wiy) {
if ($so) {
$ucut = "0";
$rcut = "0";
unset($so);
}
if (!$cuts[$ucut]) {
$so = "restart";
} else {
$pinfo = rtrim($pinfo);
$bpinfol = strlen($pinfo);
$tcut = $cuts[$ucut];
$pinfo = rtrim($pinfo,"$tcut");
$pinfol = strlen($pinfo);
if ($bpinfol == $pinfol) {
$rcut++;
if ($rcut == $lcut) {
unset($wiy);
}
$ucut++;
} else {
$so = "restart";
}
}
}
$this->cleaner = $pinfo;
}
}
$pinfo = "Well... I'm really bored...<br /><br> \n\t <br><br /><br> \r\r <br>\r<br /><br>\r \n<br> <br />\t";
$cuts = array('\n','\r','\t',' ',' ',' ','<br />','<br>','<br/>');
$pinfo = new cleaner($cuts,$pinfo);
$pinfo = $pinfo->cleaner;
print $pinfo;
?>
That class will take any string that you put in the $cust array and remove it from the end of the $pinfo string. It's useful for cleaning up comments, articles, or mail that users post to your site, making it so there's no extra blank space or blank lines.
pal
07-Dec-2004 04:53
About me at prestonhunt dot com's 20-Oct-2003 note:
What you describe is the expected bahaviour. Actually rtrim does not know whether you are using single or double quotes.
' \t' is a string with space as the 1st character, backslash as the 2nd and t as the 3rd. \t is not an escape sequence in single quoted strings. But it is in double quoted strings.
See documentation on string syntax for details.
me at prestonhunt dot com
19-Oct-2003 11:48
You have to careful when specifying the second parameter (charlist) of rtrim. It appears that you must use string interpolation (double quotes instead of single quotes) or it won't work like you expect. For instance, if you wanted to strip spaces and tabs, this would not work:
rtrim("Ends in t ", ' \t') returns "Ends in" (apparently rtrim is stripping spaces, slashes, and t's).
If you specify this instead (note use of double quotes on second parameter):
rtrim("Ends in t ", " \t") it returns the correct result ("Ends in t")
Maybe this is obvious, but I thought I would share it since it was messing me up and took some time to figure out.
todd at magnifisites dot com
19-Aug-2003 08:19
This shows how rtrim works when using the optional charlist parameter:
rtrim reads a character, one at a time, from the optional charlist parameter and compares it to the end of the str string. If the characters match, it trims it off and starts over again, looking at the "new" last character in the str string and compares it to the first character in the charlist again. If the characters do not match, it moves to the next character in the charlist parameter comparing once again. It continues until the charlist parameter has been completely processed, one at a time, and the str string no longer contains any matches. The newly "rtrimmed" string is returned.
<?php
rtrim('This is a short short sentence', 'short sentence');
rtrim('This is a short short sentence', 'cents');
?>
HW
05-Jun-2003 08:32
$text = "This string contains some unwanted characters on the end.";
$text1 = rtrim($text, 'a..z');
$text1 = rtrim($text1, '.');
echo $text1; // only the '.' is trimmed.
$text2 = rtrim($text, 'a..z.');
echo $text2; // The whole last word is trimmed.
icon-phpnet at phy dot duke dot edu
01-Apr-2002 12:00
Not entirely. Perl's "chomp" will only remove the newline character, while rtrim without the second parameter will remove ALL whitespace. E.g. chomp("blah \n") will return "blah ", while rtrim("blah \n") will return "blah".
| |