Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

php
communitycreator

What is rtrim() in PHP?

Harris Amjad

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The rtrim() function removes the trailing space characters or other specified characters from the right side of a string.

The following illustration shows a visual representation of the rtrim() function.

A visual representation of the rtrim() function

Syntax

rtrim(string,charsToBeRemoved)

Parameters

The rtrim() function takes two parameters:

  • A string from which characters need to be removed.
  • The list of characters to be removed from the right side of a string (optional).

If the value of charsToBeRemoved is not specified, then the following characters are removed from the right side of a string:

  • " " - white space
  • “\t” - tab
  • “\n” - new line
  • “\x0B” - vertical tab
  • “\r” - carriage return
  • “\0” - NULL

Return value

rtrim()returns the modified string.

Code

The example below demonstrates how to use rtrim() in PHP:

<?php
#single character removed from string
echo("rtrim('educative()','()'): ");
echo(rtrim('educative()','()'));
echo("\n");
echo("rtrim('educative!()','()'): ");
echo(rtrim('educative!()','()'));
echo("\n");
#multiple characters removed from string
echo("rtrim('educative!,()','! , ()'): ");
echo(rtrim('educative!,()','! , ()'));
echo("\n");
#default ~ without the list of characters to be removed
echo("rtrim('educative '): ");
echo(rtrim('educative '));
echo("\n");
?>

RELATED TAGS

php
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring