• 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 trim() Function – Remove Whitespace at Beginning and End of String

php trim() Function – Remove Whitespace at Beginning and End of String

March 22, 2022 Leave a Comment

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

$variable = "     this is a variable      ";

echo trim($variable);

//Output:
this is a string 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 beginning and end of a string.

To remove the whitespace at the beginning and end of a string, we 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 End of String with rtrim() in php

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

rtrim(), or “right trim”, removes the 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 at the end of a string.

$variable = "     this is a variable      ";

echo rtrim($variable);

//Output:
     this is a 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 trim() function to remove the leading and trailing spaces of a string variable.

Other Articles You'll Also Like:

  • 1.  php Absolute Value with abs Function
  • 2.  php print_r() Function- Get Information about Variables in php
  • 3.  php atan2 – Find Arctangent of the Quotient of Two Numbers
  • 4.  PHP random_int() Function – Generate Random Integers in PHP
  • 5.  php in_array – Check If Value is in php Array
  • 6.  php range() – Create Array of Elements Within Given Range
  • 7.  php Delete Files with php Unlink Function
  • 8.  php pi – Get Value of pi Using php pi() Function
  • 9.  Remove Duplicates from Array in php with array_unique()
  • 10.  php max – Find Maximum Value of Array in php

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