PHP Basics

PHP: Remove Space at the end of string

There is a string that ends with a single white space. You can use PHP trim function to remove the white space at the end of the string.

PHP trim() Function

This function not only removes white space at the end of the string but also at the beginning of the string.

<?php

echo $username;

$username=trim($username);

echo $username;

?>

Output:

“Srikant        ”

“Srikant”

PHP rtrim() Function

Alternatively, we can use rtrim function to remove white spaces at the end of the string.

<?php

echo $username;

$username=rtrim($username);

echo $username;

?>

Output:

“Srikant        ”

“Srikant”