Jump to: navigation, search

Php rtrim

From w3cyberlearnings

Contents

PHP function rtrim

This function strips whitespace from the right side of a string.

Syntax rtrim

  • string: a given string to strip whitespace

charlist: List all characters that you want to be stripped.

  • \0 -- NULL
  • \t -- Tab
  • \n -- New line
  • \x0B -- vertical tab
  • \r -- Carriage return
  • " " -- whitespace
rtrim(string, charlist);

Example 1

<?php

$str = "My friends!          ";
$str1= "Without rtrim: ". $str;
$str2= "With rtrim: ". ltrim($str);
echo $str1;
echo $str2;
?>

Output

  • HTML View Source.
			    whitespace
                         <---------->  
Without rtrim: My friends!          With rtrim: My friends! 

Example 2

<?php
$array_fruit = array('good  ', 'excellent ', 'leader ');

foreach($array_fruit as $f) {
	echo rtrim($f); 
	echo "<br/>";
}
?>

Output

  • HTML View Source.
good<br/>excellent<br/>leader<br/>

Example 3

<?php

$str ="Hello World";

$str_rtrim = rtrim($str,'World');
echo $str;
echo '<br/>';
echo $str_rtrim;
?>

Output

Hello World
Hello

Related Functions


Navigation
Web
SQL
MISC
References