PHP rtrim

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

PHP : rtrim() function

Description

The rtrim() function is used to remove the whitespaces and other predefined characters from the right side of a string.

Version

(PHP 4 and above)

Syntax

rtrim(string_name, char_list)

Parameter

Name

Description

Required /
Optional

Type

string_name

The string that will be trimmed.

Required

String

char_list

Specifies which character to remove from the string. Without this parameter, the function removs the following characters. 
" " an ordinary space 
"�" - the NUL-byte 
" - tab 
" - line feed 
x0B" - a vertical tab 
" " - a carriage return

Optional

String

Return value

Returns the modified string.

Value Type : String.

Pictorial Presentation

php-string-rtrim()

Example :

  1.  

  2. $text1="w3resource.com  ";  

  3. $text2="w3resource.com ";  

  4. $text3=" w3resource.comx0A";  

  5. $text4="Good Morning";   

  6. $trimmed_text=rtrim($text1);  

  7. var_dump($trimmed_text);  

  8. echo '
    ';  

  9. $trimmed_text=rtrim($text2);  

  10. var_dump($trimmed_text);  

  11. echo '
    ';  

  12. $trimmed_text=rtrim($text3);  

  13. var_dump($trimmed_text);  

  14. echo '
    ';  

  15. $trimmed_text=rtrim($text1,'w3');  

  16. var_dump($trimmed_text);  

  17. echo '
    ';  

  18. $trimmed_text=rtrim($text4,"ng");  

  19. var_dump($trimmed_text);  

  20. ?>  

Output :

string(14) "w3resource.com" 
string(14) "w3resource.com" 
string(16) " w3resource.com" 
string(16) "w3resource.com " 
string(10) "Good Morni"