توابع rtrim() در PHP

مثال

حذف کاراکترها از سمت راست رشته:

<?php
$str = "Hello World!";
echo $str . "<br>";
echo rtrim($str,"World!");
?>

Run Instance

تعریف و استفاده

توابع rtrim() حذف کاراکترهای خالی یا کاراکترهای پیش‌تعریف شده در سمت راست رشته را انجام می‌دهد.

توابع مرتبط:

  • ltrim() - حذف کاراکترهای خالی یا کاراکترهای پیش‌تعریف شده در سمت چپ رشته
  • trim() - حذف کاراکترهای خالی یا کاراکترهای پیش‌تعریف شده در دو طرف رشته

زبان

rtrim(string,charlist)
Parameter Description
string Required. Specify the string to be checked.
charlist

Optional. Specify which characters to remove from the string. If omitted, the following characters are removed:

  • "\0" - NULL
  • "\t" - Tab
  • "\n" - New Line
  • "\x0B" - Vertical Tab
  • "\r" - Carriage Return
  • " " - Space

Technical Details

Return Value: Return the modified string.
PHP Version: 4+
Update Log: Added in PHP 4.1 charlist Parameter.

More Examples

Example 1

Remove spaces from the right of the string:

<?php
$str = "Hello World!    ";
echo "Without using rtrim: " . $str;
echo "<br>";
echo "Use rtrim: " . rtrim($str);
?>

The HTML output of the above code is as follows (view source code):

<!DOCTYPE html>
<html>
<body>
Without using rtrim: Hello World!    <br>Use rtrim: Hello World!
</body>
</html>

The browser output of the above code is as follows:

Without using rtrim: Hello World!
Use rtrim: Hello World!

Run Instance

Example 2

Remove newline characters (\n) from the right of the string:

<?php
$str = "Hello World!\n\n\n";
echo "Without using rtrim: " . $str;
echo "<br>";
echo "Use rtrim: " . rtrim($str);
?>

The HTML output of the above code is as follows (view source code):

<!DOCTYPE html>
<html>
<body>
Without using rtrim: Hello World!
<br>Use rtrim: Hello World!
</body>
</html>

The browser output of the above code is as follows:

Without using rtrim: Hello World!
Use rtrim: Hello World!

Run Instance