PHP Questions Home | Register | Login
Categories
Arrays
Files
Forms
Creating PDFs
PHP Errors
Functions
Images
MySQL
Numbers
Others
Strings
Website
Unanswered

PHP Forums
Forums Index
MySQL Forum
PHP Forum
Flash/Actionscript
Javascript
Web design

More PHP
PHP Functions
Top Questions
Ask a Question
PHP Interview Questions
PHP Jobs
Ecommerce
IT Questions
Business Websites

Sponsors

sponsor ad
My Salary
PHP Developer Salary
Advertise Here

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.



Comment on this Question and Answer >>>

ASK A QUESTION

More strings PHP Questions

How do I convert newlines to HTML line break tags?

How do I replace a word in a string in PHP?

How do I find the last occurrence of a character in a string?

How can I repeat a string variable several times?

7 Using regular expressions check for the validity of entered email-id. The @ symbol should not appear more than once. The dot (.) can appear at the most once before @ and at the most twice or at least once after @ symbol. The substring before @



Custom Search