PHP ltrim() Function

Quick summary: The PHP ltrim() function removes whitespace or specified characters from the beginning of a string.

PHP ltrim() Syntax

ltrim(string $string, string $characters = " \n\r\t\v\x00"): string
PHP

PHP ltrim() Basic examples

$text = "   Hello world";
echo ltrim($text);
PHP
Output:
Hello world

Removes leading whitespace from a string.

$text = "***Hello***";
echo ltrim($text, "*");
PHP
Output:
Hello***

Removes specified characters only from the beginning.

$text = "\n\tHello";
echo ltrim($text);
PHP
Output:
Hello

Removes newlines and tabs from the start of the string.

PHP ltrim() Real-world usage

$path = "/usr/local/bin";
echo ltrim($path, "/");
PHP
Output:
usr/local/bin

Removes a leading slash when normalizing file paths.

$input = "   admin";
$username = ltrim($input);
echo $username;
PHP
Output:
admin

Cleans user input without touching trailing spaces.

PHP ltrim() Edge cases

$text = "";
echo ltrim($text);
PHP

Calling ltrim() on an empty string returns an empty string.

$text = "   ";
echo ltrim($text);
PHP

A string containing only whitespace becomes empty.

PHP ltrim() Common mistakes

Assuming ltrim removes characters from the end

ltrim() only affects the beginning of the string.

Incorrect
echo ltrim("Hello***", "*");
Correct
echo rtrim("Hello***", "*");

Use rtrim() to remove characters from the end.

Forgetting to specify a character mask

By default, ltrim() removes only whitespace.

Incorrect
echo ltrim("***value***");
Correct
echo ltrim("***value***", "*");

Provide a character mask to remove non-whitespace characters.

PHP ltrim() Frequently Asked Questions

What does ltrim() do in PHP?

ltrim() removes whitespace or specified characters from the beginning of a string.

What characters does ltrim() remove by default?

By default, ltrim() removes spaces, tabs, newlines, carriage returns, vertical tabs, and null bytes from the start of a string.

What is the difference between ltrim() and trim()?

ltrim() removes characters only from the beginning, while trim() removes from both the beginning and the end of a string.

What is the difference between ltrim() and rtrim()?

ltrim() removes characters from the beginning of a string, while rtrim() removes them from the end.

How to remove specific characters using ltrim()?

Pass a character mask as the second argument, for example ltrim($string, "*") to remove asterisks from the beginning.

Does ltrim() remove spaces in the middle of a string?

No, ltrim() only removes characters from the start of a string and does not affect characters in the middle.

Does ltrim() modify the original string?

No, ltrim() returns a new string and does not modify the original variable unless reassigned.

Can ltrim() remove numbers or letters?

Yes, if specified in the character mask, ltrim() can remove any characters including numbers and letters from the start.

What happens if the string is empty?

ltrim() returns an empty string when applied to an empty string.

Does ltrim() work with multibyte strings?

No, ltrim() is not multibyte-safe. Use appropriate functions or regex for Unicode strings.

Is ltrim() safe for cleaning user input?

Yes, ltrim() is useful for cleaning leading whitespace in user input, but should be combined with validation and sanitization.

What is a real-world use case of ltrim()?

ltrim() is commonly used to remove unwanted leading characters like spaces, slashes, or symbols when processing user input or file paths.

PHP ltrim() Related PHP Functions