PHP trim functions: trim, rtrim, and ltrim

String variables are common in any programming language. You are likely going to use or manipulate a string at some point while programming. Sometimes, string variables are not at all perfect. Many of them may even have characters that need to be removed. Targeting specific locations of a string, PHP provides 3 string functions we can use to remove unwanted characters from a string variable: trim(), ltrim() and rtrim(). Continue reading to see examples of each function…

computer screen fade out of computer code
Image by Pexels from Pixabay

Self-Promotion:

If you enjoy the content written here, by all means, share this blog and your favorite post(s) with others who may benefit from or like it as well. Since coffee is my favorite drink, you can even buy me one if you would like!


PHP trim functions: trim()

A space is a character and can be targeted with any of the 3 string functions I am covering in this post. It’s a common practice to sanitize any user-input data from a web form, including the removal of any whitespace (blanks and spaces) from both the beginning and end of a string. In this case, we can use trim().

trim() accepts 1 required parameter – the target string – and 1 optional parameter, the character(s) to remove. If the optional 2nd parameter is not specified, trim() removes these characters:

  • “\0” – NULL
  • “\t” – tab
  • “\n” – new line
  • “\x0B” – vertical tab
  • “\r” – carriage return
  • ” ” – ordinary white spac
(source for the above list is here)

Since whitespace is difficult to see in browser output, I’ll use the strlen() function and provide the length of an example string, that is prefixed with a single space character, before and after using trim() on the string variable:

1
2
3
$first_name = ' Josh';
echo strlen($first_name);
5

With the single space character prefixing the name ‘Josh’, strlen() returns a length of 5 for this string variable.

Calling trim() on the ‘$first_name’ variable both specifying a single space as the character to remove and omitting it, yields the same result since a space is one of the default characters that are removed if none are specified:

1
2
3
4
5
6
7
$first_name = trim(' Josh');
echo strlen($first_name);
4

$first_name = trim(' Josh', ' ');
echo strlen($first_name);
4

trim() can remove 2 spaces from the end of the string as shown in this next example:

1
2
3
$first_name = 'Josh  ';
echo strlen($first_name);
6

1
2
3
$first_name = trim('Josh  ');
echo strlen($first_name);
4

Even without specifying the 2 spaces as the character to remove, trim() still removes both of them no problem.

PHP trim functions: rtrim()

The rtrim() function removes whitespace or other characters from the end of a string variable. Just as with trim(), if the optional second parameter is not provided in the rtrim() function call, the same list of characters (shown in the trim() section) is stripped from the end of the target string.

My name is ‘Joshua’ but most folks call me ‘Josh’. Suppose I have filled out a form as ‘Joshua’ but I want my name stored as ‘Josh’. I can easily remove the ‘ua’ characters from the end of my name using rtrim() as shown in the image following the below code:

1
2
3
$first_name = 'Joshua';
echo $first_name.'<br />';
echo rtrim($first_name, 'ua');

browser output of PHP rtrim function
Browser output of PHP rtrim() function.

PHP trim functions: ltrim()

And as you may have guessed it, ltrim() removes whitespace or other characters from the beginning of a string. The same rules apply to ltrim() as those for trim() and rtrim() in accordance with the optional 2nd parameter.

Suppose I have the characters ‘Sir ‘ prefixed to my name, ‘Joshua’. I can easily remove those 4 characters by specifying them as the 2nd parameter to ltrim(). See the screenshot below for the browser output from the following code snippet:

1
2
3
$first_name = 'Sir Joshua';
echo $first_name.'<br />';
echo ltrim($first_name, 'Sir ');

browser output of php ltrim function
Browser output of PHP ltrim() function.

Informational Resources

Visit the below links to the official PHP documentation on trim(), rtrim(), and ltrim() for more information:

Like what you have read? See anything incorrect? Please comment below and thank you for reading!!!

A Call To Action!

Thank you for taking the time to read this post. I truly hope you discovered something interesting and enlightening. Please share your findings here, with someone else you know who would get the same value out of it as well.

Visit the Portfolio-Projects page to see blog post/technical writing I have completed for clients.



To receive email notifications (Never Spam) from this blog (“Digital Owl’s Prose”) for the latest blog posts as they are published, please subscribe (of your own volition) by clicking the β€˜Click To Subscribe!’ button in the sidebar on the homepage! (Feel free at any time to review the Digital Owl’s Prose Privacy Policy Page for any questions you may have about: email updates, opt-in, opt-out, contact forms, etc…)

Be sure and visit the “Best Of” page for a collection of my best blog posts.


Josh Otwell has a passion to study and grow as a SQL Developer and blogger. Other favorite activities find him with his nose buried in a good book, article, or the Linux command line. Among those, he shares a love of tabletop RPG games, reading fantasy novels, and spending time with his wife and two daughters.

Disclaimer: The examples presented in this post are hypothetical ideas of how to achieve similar types of results. They are not the utmost best solution(s). The majority, if not all, of the examples provided, are performed on a personal development/learning workstation-environment and should not be considered production quality or ready. Your particular goals and needs may vary. Use those practices that best benefit your needs and goals. Opinions are my own.

Hey thanks for commenting! Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.