PHP Questions Home

Categories

Arrays
Files
Forms
Functions
Images
MySQL
Numbers
Others
Strings
Website


PHP Functions

PHP Functions


More PHP

Top Questions
Ask a Question

How do I remove trailing whitespace from a string?


If you want to do this then it's easy, with the rtrim function.

You've used ltrim to remove space from the left and you've used just good old trim on its to remove space from both ends, and therefore it makes sense that you use rtrim to remove it from the right side.

Just in case you're not sure how to use it, here is a very simple example indeed:

<?php
$str 
"I have right space   ";
echo 
"<$str>\n";
echo 
"<" rtrim($str) . ">";

//<I have right space   >
//<I have right space>
?>


As you can see from the angle brackets, the space at the right of the string has been removed from the second printint of the string. Note that if there had been leading whitespace, it would have remained.



More strings PHP Questions

How do I calculate the metaphone of a string?
How do I calculate the MD5 hash of a string?
What is the difference between print and echo?
How do I make the first letter of each word upper case?
How do I fetch the meta tags from a webpage using PHP?