rtrim
STRIP whitespaces and selected characters from the end of a STRING.
This function is an alias to
chop.
chop
STRIP whitespaces and selected characters from the end of a STRING.
This function is an alias to
rtrim.
This function returns a string with whitespace stripped from the end of $str if $character_mask is not provided.
<?php
str rtrim ( str $str [, str $character_mask ] )
or
str chop ( str $str [, str $character_mask ] )
where,
$str = The input STRING
$character_mask = The list of additional characters
to strip from the end
?>
$str
The input STRING.
$character_mask
The additional characters list to be striped:
DEFAULT |
ASCII |
VALUE |
" " |
32 |
0x20 |
ESPAÇO |
"\t" |
9 |
0x09 |
TAB H |
"\n" |
10 |
0x0A |
NL |
"\r" |
13 |
0x0D |
CR |
"\0" |
0 |
0x00 |
NULL |
"\x0B" |
11 |
0x0B |
TAB V |
ed48 |
EXERCISE
<?php
$strl01a = ' QUOSQUE TANDEM? ';
$strl01b = rtrim($strl01a);
echo $strl01b . '<br><br>';
$strl01c = ' QUOSQUE TANDEM? ';
$strl01d = rtrim($strl01c);
echo $strl01d;
?>
RESULT
QUOSQUE TANDEM?
QUOSQUE TANDEM?
EXERCISE
<?php
$strl02a = '-/--//---///----QUOSQUE TANDEM?-/--//---///----';
$strl02b = rtrim($strl02a, '"-", "/"');
echo $strl02b . '<br><br>';
/* ---------------------------------------
list of characters defined by
a range of ASCII hexadecimal values
'0x29,..0x30' -> from 0x29 to 0x30
--------------------------------------- */
$strl02c = '-/--//---///----QUOSQUE TANDEM?-/--//---///----';
$strl02d = rtrim($strl02c, '0x29, ..0x30');
echo $strl02d;
?>
RESULT
-/--//---///----QUOSQUE TANDEM?
-/--//---///----QUOSQUE TANDEM?
EXERCISE
<?php
/* Testing the Normal behaviour
of rtrim() function */
echo "<br><br>Output for Normal Behaviour:<br>";
var_dump ( rtrim("rtrim test \t\0 ") );
/* without second Argument */
echo "<br><br>";
var_dump ( rtrim("rtrim test " , "") );
/* no characters in second Argument */
echo "<br><br>";
var_dump ( rtrim("rtrim test ", NULL) );
/* with NULL as second Argument */
echo "<br><br>";
var_dump ( rtrim("rtrim test ", true) );
/* with boolean value as second Argument */
echo "<br><br>";
var_dump ( rtrim("rtrim test ", " ") );
/* with single space as second Argument */
echo "<br><br>";
var_dump ( rtrim("rtrim test \t\n\r\0\x0B", "\t\n\r\0\x0B") );
/* with multiple escape sequences as second Argument */
echo "<br><br>";
var_dump ( rtrim("rtrim testABCXYZ", "A..Z") );
/* with characters range as second Argument */
echo "<br><br>";
var_dump ( rtrim("rtrim test0123456789", "0..9") );
/* with numbers range as second Argument */
echo "<br><br>";
var_dump ( rtrim("rtrim test$#@", "#@$") );
/* with some special characters as second Argument */
/* Use of class and objects */
echo "<br><br>Checking with OBJECTS:<br>";
class string1 {
public function __toString() {
return "Object";
}
}
$obj = new string1;
var_dump( rtrim($obj, "tc") );
/* String with embedded NULL */
echo "<br><br>String with embedded NULL:<br>";
var_dump( rtrim("234\x0005678\x0000efgh\xijkl\x0n1", "\x0n1") );
/* heredoc string */
$str = <<<EOD
us
ing heredoc string
EOD;
echo "<br><br>Using heredoc string:<br>";
var_dump( rtrim($str, "ing") );
?>
EXERCISE
<?php
echo "Testing rtrim() : basic functionality.<br>";
$text = "---These are a few words--- \t\r\n\0\x0B ";
$hello = "!===Hello World===!";
$alpha = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
$binary = "Example string\x0A\x0D";
echo "<br><br>Trim string with all white space characters:<br>";
var_dump(rtrim($text));
echo "<br><br>Trim non-whitespace from a string:<br>";
var_dump(rtrim($hello, "=!"));
echo "<br><br>Trim some non-white space characters
from a string:<br>";
var_dump(rtrim($hello, "!dlWro="));
echo "<br><br>Trim some non-white space characters
from a string using a character range:<br>";
var_dump(rtrim($alpha, "A..Z"));
echo "<br><br>Trim the ASCII control characters
at the beginning of a string:<br>";
var_dump(rtrim($binary, "\x00..\x1F"));
?>
EXERCISE
<?php
echo "Testing rtrim() : error conditions.<br>";
$hello = " Hello World\n";
echo "<br><br>Test rtrim function
with various invalid charlists\n";
var_dump(rtrim($hello, "..a"));
echo "<br><br>";
var_dump(rtrim($hello, "a.."));
echo "<br><br>";
var_dump(rtrim($hello, "z..a"));
echo "<br><br>";
var_dump(rtrim($hello, "a..b..c"));
?>
EXERCISE
<?php
/*
* Testing chop() = rtrim(() :
* with nulls embedded in input string
*/
echo "Testing chop() : string with embedded nulls.<br>";
// defining varous strings with embedded nulls
$strings_with_nulls = array(
"hello\0world",
"\0hello",
"hello\0",
"\0\0hello\tworld\0\0",
"\\0hello\\0",
'hello\0\0',
chr(0),
chr(0).chr(0),
chr(0).'hello'.chr(0),
'hello'.chr(0).'world'
);
$count = 1;
foreach($strings_with_nulls as $string) {
echo "<br><br>Iteration: $count<br>";
var_dump( chop($string) );
var_dump( chop($string, "\0") );
var_dump( chop($string, '\0') );
$count++;
}
?>