There are a lot of times when we want to work with strings but they are padded with unwanted characters either at the beginning or the end. PHP has defined specific functions that we can use to trim such strings easily.
In this quick tip, we will discuss to common string trimming situations that you will likely encounter. The first one involves removing specific characters from either one or both ends of a string. The second one involves trimming strings based on how many characters we want to remove from either end.
Trimming Strings to Remove a Set of Characters
There are three different trimming functions in PHP called ltrim()
, rtrim()
and trim()
. They remove either whitespace or any other set of characters from the left end, right end or both ends respectively.
If you don't mention as list of characters to remove as the second parameter, these function will remove whitespace characters like spaces (" "
), tabs ("\t"
), new lines ("\n"
), carriage returns ("\r"
), null bytes ("\0"
) and vertical tabs ("\v"
).
There is no limit to the number of characters that will be trimmed by these functions. In other words, they will keep trimming the string as long as it contains any of the specified characters at either end.
Here are a few examples of these functions. The main strings that we are trimming will contain a combination of whitespace, tabs or newline characters at both ends.
<?php $strings = [" Hi, are you alright? ", " I am going for a walk. ", " That was amazing!!! "]; foreach($strings as $string) { var_dump(ltrim($string)); } /* Output from ltrim() string(25) "Hi, are you alright? " string(23) "I am going for a walk. " string(25) "That was amazing!!! */ foreach($strings as $string) { var_dump(rtrim($string)); } /* Output from rtrim() string(22) " Hi, are you alright?" string(25) " I am going for a walk." string(24) " That was amazing!!!" */ foreach($strings as $string) { var_dump(trim($string)); } /* Output from trim() string(20) "Hi, are you alright?" string(22) "I am going for a walk." string(19) "That was amazing!!!" */ ?>
As you can see, ltrim()
removes all the whitespace form the left of the strings while keeping the right end untouched. Similarly, rtrim()
removes all the whitespace from the right of the strings while keeping the left end untouched.
It is important to keep in mind that trimming only happens when the characters are exactly at the end or beginning. Here is an example:
<?php $string = "I am doing very good! How are you? "; var_dump(trim($string)); /* string(51) "I am doing very good! How are you?" */ ?>
You can also trim any other set of characters if you pass them as second parameter of ltrim()
, rtrim()
or trim()
.
<?php $string = " 123131311211 I am doing very good! How are you? 1231312112 12132321"; var_dump(trim($string, "123")); /* string(65) " 123131311211 I am doing very good! How are you? 1231312112 " */ ?>
This time we had specified that we only want to trim the characters 1,2 and 3. Therefore, trim()
left the whitespace untouched.
<?php $string = "12121231409874509121208409812112128311213"; var_dump(trim($string, "123")); /* string(27) "409874509121208409812112128" */ ?>
Similarly, in this second example, trim
stopped removing digits from the both ends as soon as it found a mismatch from the characters we asked it to trim.
<?php $string = "12121231409874509121208409812112128311213"; var_dump(trim($string, "1..9")); /* string(16) "0987450912120840" */ ?>
This third example demonstrates how we can use (..
) to specify a range of characters that we want trimmed. This way we can avoid listing all the characters if they occur sequentially.
Trimming Strings to Remove a Specific Number of Characters
Just like trimming a specific set of characters, PHP also has dedicated functions to trim a specific number of characters.
The substr($string, $offset, $length)
function is best suited for this job. It accepts three parameters and returns a substring. The first parameter is the string you want to trim. The $offset
parameter determines the start of the returned substring and the $length
parameter determines the length of the substring.
We can control the values of $offset
and $length
to trim our main string based on number of characters.
- You can trim the string from the left side by only specifying an offset and omitting the
$length
parameter when callingsubstr()
. In this case,substr()
will return a substring starting from offset until the end of the string. - You can trim the string from the right side by setting the value of
$offset
to 0 and making$length
negative. This will remove$length
number of characters from the right end of the main string. - You can specify a non-zero positive value for
$offset
and a negative value for$length
to makesubstr()
act liketrim()
and remove a specific number of characters form both ends.
Here is an example that trims a string using the substr()
function:
<?php $string = "I will try to trim this sentence in different ways."; var_dump(substr($string, 10)); // string(41) " to trim this sentence in different ways." var_dump(substr($string, -10)); // string(41) "I will try to trim this sentence in diffe" var_dump(substr($string, 10, -10)); // string(31) " to trim this sentence in diffe" ?>
Final Thoughts
In this quick tip, we learned how to trim strings in PHP based on a specific set or a specific number of characters. If you have any questions or tips related to the post, feel free to post them in the comments.
Learn PHP With a Free Online Course
If you want to learn PHP, check out our free online course on PHP fundamentals!
In this course, you'll learn the fundamentals of PHP programming. You'll start with the basics, learning how PHP works and writing simple PHP loops and functions. Then you'll build up to coding classes for simple object-oriented programming (OOP). Along the way, you'll learn all the most important skills for writing apps for the web: you'll get a chance to practice responding to GET and POST requests, parsing JSON, authenticating users, and using a MySQL database.
-
FREEPHPPHP Fundamentals
No comments:
Post a Comment