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:
rtrim(). Continue reading to see examples of each function…
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() 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
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:
$first_name = ' Josh';
With the single space character prefixing the name ‘Josh’,
strlen() returns a length of 5 for this string variable.
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:
$first_name = trim(' Josh');
$first_name = trim(' Josh', ' ');
trim() can remove 2 spaces from the end of the string as shown in this next example:
$first_name = 'Josh ';
$first_name = trim('Josh ');
Even without specifying the 2 spaces as the character to remove,
trim() still removes both of them no problem.
PHP trim functions: rtrim()
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:
$first_name = 'Joshua';
echo $first_name.'<br />';
echo rtrim($first_name, 'ua');
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
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:
$first_name = 'Sir Joshua';
echo $first_name.'<br />';
echo ltrim($first_name, 'Sir ');
Visit the below links to the official PHP documentation on
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.
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.