• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / PHP / php rtrim() Function – Remove Whitespace at End of String

php rtrim() Function – Remove Whitespace at End of String

March 22, 2022 Leave a Comment

In php, the rtrim() function is very useful if we have strings with unwanted whitespace. With the php rtrim() function, we can remove whitespace from the end of a string.

$variable = "     this is a variable      ";

echo rtrim($variable);

//Output:
     this is a variable

When working with string variables in our php programs, it is useful to be able to easily manipulate and change the value of the variables. One such change is if you want to trim the whitespace from the end of a string.

To remove the whitespace at the end of a string, we can use the php rtrim() function.

rtrim(), or “right trim”, removes the trailing spaces at the end of a string variable.

Below is a simple example of how to use the rtrim() function in php to remove the spaces from the end of a string.

$variable = "     this is a variable      ";

echo rtrim($variable);

//Output:
     this is a variable

Removing Whitespace from Beginning and End of String with php trim() Function

If you are looking to remove both the leading and trailing leading spaces and get rid of all the whitespace at the beginning and end of a string, you can use the php trim() function.

trim() takes in a string and gets rid of the leading and trailing spaces.

Below is a simple example of how to use the trim() function in php to remove the spaces from the beginning and end of a string.

$variable = "     this is a variable      ";

echo trim($variable);

//Output:
this is a string variable

Removing Whitespace from Beginning of String with ltrim() in php

If you are looking to remove only the leading spaces and get rid of all the whitespace at the beginning of a string, you can use the php ltrim() function.

ltrim(), or “left trim”, removes the spaces at the beginning of a string variable.

Below is a simple example of how to use the ltrim() function in php to remove the spaces at the beginning of a string.

$variable = "     this is a variable      ";

echo ltrim($variable);

//Output:
this is a variable      

Hopefully this article has been useful for you to learn how to use the php rtrim() function to remove the trailing spaces of a string variable.

Other Articles You'll Also Like:

  • 1.  Get User Agent in php with $_SERVER[‘HTTP_USER_AGENT’]
  • 2.  Remove Last Character from String Using php
  • 3.  php array_fill() – Create Array of Length N with Same Value
  • 4.  How to Get Session ID in php with session_id()
  • 5.  Capitalize First Letter of String with php ucfirst() Function
  • 6.  php in_array – Check If Value is in php Array
  • 7.  php sleep() Function – Pause Execution of Code in php
  • 8.  What is the Difference Between unset() and unlink() in php?
  • 9.  php pi – Get Value of pi Using php pi() Function
  • 10.  php cos – Find Cosine of Number in Radians Using php cos() Function

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy