How to use rtrim in PHP

In this article I will explain how the rtrim() function can be used in PHP.
  • 1518

rtrim() function in PHP

  • The rtrim() function is used to remove white spaces or other predefined character from the right side of a string.
  • The rtrim() function returns a string without white-space.

Syntax

rtrim(string,charlist)

Parameter

  • string string is required parameter. It determine check to string.
  • charlist charlist is optional parameter. it's determine for characters to remove from the string. If omitted, all of the following characters are removed:
    • "\0" - NULL
    • "\t" - tab
    • "\n" - new line
    • "\x0B" - vertical tab
    • "\r" - carriage return
    • " " - ordinary white space

Example

The following example show to how the rtrim() function can be used in PHP.

<html>

<body>

<h3 style="color: green;">rtrim() function example in PHP</h3>

    <?php

    $string1="C-sharpcorner";

    $string2="C-sharpcorner\t\t";

    $string3="\t\tC-sharpcorner\x0A";

    $string4="Have A Nice Day";

    $trimmed_text=rtrim($string1);

    var_dump($trimmed_text);

    echo '<br>';

    $trimmed_text=rtrim($string2);

    var_dump($trimmed_text);

    echo '<br>';

    $trimmed_text=rtrim($string3);

    var_dump($trimmed_text);

    echo '<br>';

    $trimmed_text=rtrim($string1,'C-');

    var_dump($trimmed_text);

    echo '<br>';

    $trimmed_text=rtrim($string4,"ay");

    var_dump($trimmed_text);

    ?> 

</body>

</html>

 

Output

rtrim-php.gif
You may also want to read these related articles here
 
Ask Your Question 
 
Got a programming related question? You may want to post your question here
 

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.