PHP strip remove white space from end of string

Need to remove the white space at the end of a string before recording the data. Use PHP’s rtrim().


rtrim is great for deleting that unwanted space at then end of strings. The perfect example for using this function is when receiving data from an online form and, at the end of a data field, some one leaves an extra white space. This white space means nothing and you want to delete it. rtrim() to the rescue.


$password = "123 ";
$password = rtrim($password);
// $password = "123"

Other Examples:

You can also trim down a string by matching words or letters and the end of a string.


// Example 1: Character-by-character comparison match.
echo rtrim('This is a test test code', 'test code');
// returns 'This is a'
// Example 2: matches the last whole word with half the letters
echo rtrim('This is a test', 'est');
// returns 'This is a'

You may also like...

2 Responses

  1. Joseph says:

    Very nice. I forgot the code and I find it here. Thanks.

  2. I found this to be very useful, thanks so much! I had been looking for hours for something to do this :D.

Leave a Reply