PHP ltrim() Function

What is PHP ltrim() Function?

If you want to remove whitespaces and other characters from the left side of a string, use ltrim() function. It is very important to remove whitespaces from a string. For example, a user adds a space in the beginnings of his email address. Later, when you’ll send an email, he’ll never get the message.

Note, a whitespace is any character that renders a space. It includes – an ordinary space character, a tab character, a new line (line feed) character, a carriage return character, a NUL-byte character, a vertical tab character.

Syntax:

ltrim(string, characters)

Parameters:

The Function has 1 required and 1 optional parameter-

string (Required): The string that is to be trimmed. Check example 1. If you don’t mention the second parameter, the ltrim() function actually removes the following characters-

  • An ordinary space  – “ ” which is
  • A tab – “\t”
  • A new line (line feed) – “\n”
  • A carriage return – “\r”
  • The NUL-byte – “\0”
  • A vertical tab – “\t”

characters (Optional): A list of characters that is to be removed from the beginning of the “string” parameter.

  • Mention all the characters that you want to trim. Check example 2.
  • If you want to remove a range of characters, use “..”.  Ex. “i..p”, “A..M” etc. For example, if you want to remove all the character from a to m, specify “a..m”. Check example 3.

Return Values:

The function returns the trimmed string.

How ltrim() function works:

When using with just one parameter (the string), the function removes any space, tab, new line, carriage return, NUL-byte, vertical tab from the left side of the string.

When using with the two parameters, it removes all the characters from the left side of the first parameter. The function removes the characters listed in second parameter recursively from the left side of the string as long as it finds any mismatching characters. Check example 4.

Examples:

Example 1: Trimming whitespaces with ltrim() function-

<pre>
<?php
$string   = " contact@Schoolsofweb";
echo "Before trimming: '$string'" . "<br />";
$afterTrimming = ltrim($string);
echo "After trimming: '$afterTrimming'";
?>
</pre>

Output:

Before trimming: ‘ contact@Schoolsofweb’
After trimming: ‘contact@Schoolsofweb’

Explanation:

When the ltrim() function uses just one parameter, it removes all the whitespaces from the string.

Example 2: Trimming specific characters with ltrim() function-

<pre>
<?php
$string   = "Check All courses at Schools of web";
echo "Before trimming: '$string'" . "<br />";
$afterTrimming = ltrim($string, "cCehk ");
echo "After trimming: '$afterTrimming'";
?>
</pre>

Output:

Before trimming: ‘Check All courses at Schools of web’
After trimming: ‘All courses at Schools of web’

Explanation:

It removes “c”, capital “C”, “e”, “h”, “k” and space (“ ”) from the string.

Example 3: Trimming a range of characters with ltrim() function-

<pre>
<?php
$string   = "oopsStart with PHP for Beginners";
echo "Before trimming: '$string'" . "<br />";
$afterTrimming = ltrim($string, "o..s");
echo "After trimming: '$afterTrimming'";
?>
</pre>

Output:

Before trimming: ‘oopsStart with PHP for Beginners’
After trimming: ‘Start with PHP for Beginners’

Explanation:

It removes all the characters from “o” to “s”.

Example 4: ltrim() function trimming characters recursively-

<pre>
<?php
$string   = "==>In-depth knowledge in CSS";
echo "Before trimming: '$string'" . "<br />";
$afterTrimming = ltrim($string, ">=");
echo "After trimming: '$afterTrimming'";
?>
</pre>

Output:

Before trimming: ‘==>In-depth knowledge in CSS’
After trimming: ‘In-depth knowledge in CSS’

Explanation:

Three characters “=”, and “>” are to remove from the string. At first, it removes “=” from the beginning of the string. After that, “=” becomes the first character of the string and the function again trim this character and continues this process.

Example 5: Trimming similar characters from a pile of data with ltrim() function-

<pre>
<?php
$languages   = array("onePHP", "twoPython", "threeRuby");
for($i=0; $i<count($languages); $i++){
    echo "Before trimming: '$languages[$i]'";
    $afterTrimming = ltrim($languages[$i], "a..z");
    echo "After trimming: '$afterTrimming'" . "<br />";
}
?>
</pre>

Output:

Before trimming: ‘onePHP’After trimming: ‘PHP’
Before trimming: ‘twoPython’After trimming: ‘Python’
Before trimming: ‘threeRuby’After trimming: ‘Ruby’

Explanation:

ltrim() function can remove similar character (small letters) from a large pile of strings.

Example 6: Trimming selected whitespaces from the second parameter with ltrim() function-

<pre>
<?php
$string   = "\t\nSchools of web";
echo "Before trimming: '$string'" . "<br />";
$afterTrimming = ltrim($string, " ");
echo "After trimming: '$afterTrimming'";
echo "<br />";
echo "Before trimming: '$string'" . "<br />";
$afterTrimming = ltrim($string, "\t\n");
echo "After trimming: '$afterTrimming'";
?>
</pre>

Output:

Before trimming: ‘   

Schools of web’
After trimming: ‘      

Schools of web’
Before trimming: ‘    Schools of web’
After trimming: ‘Schools of web’

Explanation:

When you use second parameter, the ltrim() function doesn’t remove any whitespaces automatically unless you mention that in the second parameter. So, the function in line 5 doesn’t remove any whitespaces (tab, new line), but, it does in line 9.

Example 7: Case sensitive trimming with ltrim() function-

<pre>
<?php
$string   = "ABCabc";
echo "Before trimming: '$string'";
$afterTrimming = ltrim($string, "abc");
echo " After trimming: '$afterTrimming'";
echo "<br />";
echo "Before trimming: '$string'";
$afterTrimming = ltrim($string, "ABC");
echo " After trimming: '$afterTrimming'";
?>
</pre>

Output:

Before trimming: ‘ABCabc’ After trimming: ‘ABCabc’
Before trimming: ‘ABCabc’ After trimming: ‘abc’

Explanation:

The ltrim() function removes characters case-sensitively. So, when you mention small letter “abc” in the second parameter to remove, the function doesn’t remove capital “ABC” from the string in line 5, but, when you mention capital “ABC” as the second parameter in line 9, the function removes this time.

Example 8: Trimming multiple characters, whitespaces together with ltrim() function-

<pre>
<?php
$string   = "HTML\tCSS\tJS\nPYTHON\tRUBY129";
echo "Before trimming: '$string'" . "<br />";
$afterTrimming = ltrim($string, "1..9,A..Z,\t,\n");
echo "After trimming: '$afterTrimming'";
?>
</pre>

Output:

Before trimming: ‘HTML           CSS            JS PYTHON              RUBY129’
After trimming: ”

Explanation:

To remove multiple types of characters like whitespaces, normal characters, numbers, you’ll use comma to separate those in the second parameter.

Practical Usages of ltrim() Function:

Few of the applications of the ltrim() function are-

  • The function is very useful to sanitize user inputs. It eliminates unnecessary spaces, tabs, carriage returns, newline etc. from user inputs. For example, users may add extra space when supplying username, email, comment etc. This function trims these unnecessary characters in the server side.
  • The function is also very useful to sanitize data that are found from external sources like external files, database etc.
  • To trim similar types of characters from the left side of the string, you can use ltrim() function. For example, you have a large excel file which has a string column. Each column contains one word that starts with a capital letter. But there are few unnecessary small letters before the word. Use this function to remove the unnecessary small letters from the beginning. Check example 5.

Notes on ltrim() Function:

  • When you mention the second parameter, the ltrim() function doesn’t remove the whitespaces from the string automatically that it does when you use just one parameter. In this case, you’ve to mention the list of whitespace characters you want to remove sin the second parameter. Check example 6.
  • The characters you mention in the second parameter work case-sensitively. Which means, if you enlist “S” (Capital letter), the function will remove “s” (Small letter) not “S” (Capital letter) from string. Check example 7.
  • You can mention characters, multiple whitespace characters in the second parameter with comma. Check example 8.

PHP Version Support:

PHP 4, PHP 5, PHP 7, PHP 8

Summary: PHP ltrim() Function

trim() is one of the frequently used built-in PHP string functions. By removing extra spaces, tabs etc., it ensures data consistency and makes coding smooth.

Reference:

https://www.php.net/manual/en/function.ltrim.php