PHP ltrim() and rtrim() Functions with Examples
In this tutorial, we will learn about ltrim() and rtrim() functions in PHP with the help of examples. Let’s begin understanding about the ltrim() function.
PHP ltrim() Function
The ltrim() function in PHP is a built-in function which is used to remove whitespace or specified characters from the beginning (i.e. left end) of a string. The general syntax to define ltrim() function in PHP is as follows:
ltrim(string $string, string $charlist): string
In the above syntax, there are two parameters named $string and $charlist.
- $string is the input string you want to trim. This is a required parameter.
- $charlist specifies the list of characters you want to remove from the left end of the string. If you don’t specify this, PHP will remove all whitespace characters by default.
- Space (” “)
- Newline (\n)
- Carriage return (\r)
- Tab (\t)
- Vertical tab (\v)
- NULL byte (\0)
Return Value
The ltrim() function returns the trimmed string with specified characters removed from left end. In other words, this function will return a new string value after trimming from the beginning.
Basic Examples of ltrim() Function
Example 1: Let’s write a PHP program in which we will trim leading spaces from the left end of a string.
<?php
$text = " Hello, PHP!";
$result = ltrim($text);
echo $result;
?>
Output: Hello, PHP!
In this example, we have defined a string named $text and assigned a value to it. This value contains whilespaces at the left end of the string. Then, we have called ltrim() function to remove whitespaces from the left end of the string and displayed the result.
Example 2: Let’s write a PHP program in which we will remove a specific character (!!!) from the left end of a string using ltrim() function.
<?php
$text = "!!!PHP Programming!!!";
$result = ltrim($text, "!");
echo $result;
?>
Output: PHP Programming!!!
Example 3: Let’s write a program in PHP where we will trim numbers from the left end of a string.
<?php
$text = "123456abc";
$result = ltrim($text, "123456");
echo $result;
?>
Output: abc
PHP rtrim() Function
The rtrim() function provided by PHP is a built-in function which is used to remove whitespace or specified characters from the right end of a string. The general syntax to define rtrim() function is as follows:
rtrim(string $string, string $characters): string
In the above syntax, the parameter $characters is a list of characters to be removed from the right end of a string. However, this is an optional parameter.
Return Value
The rtrim() function returns the trimmed string with specified characters removed from the right end.
Basic Examples of rtrim() Function
Example 1: Let’s write a PHP program in which we will remove whitespaces from the right end of the string using rtrim() function.
<?php
$text = "Hello, PHP ";
$result = rtrim($text);
echo $result;
?>
Output: Hello, PHP
Example 2: Let’s create a program in PHP in which we will trim newline characters from the right end of the string using rtrim() function.
<?php
$text = "Scientech Easy \n\n\n";
$result = rtrim($text);
echo $result;
?>
Output: Scientech Easy
Here, you learned about ltrim() and rtrim(0 functions in PHP which is used to remove characters from the left and right ends of a string, respectively. These are powerful functions for manipulating strings. I hope that you will have understood the basic use of ltrim() and rtrim(0 function and practiced all examples based on them.