PHP ltrim() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

In PHP, ltrim() removes whitespace, or other characters you choose, from the start of a string.

The end of the string is not touched. Use rtrim() for the end or trim() for both sides.

Syntax

Call ltrim() with the values you need. Start with this line.

PHP
$clean = ltrim("   Hello");
CallWhat it does
ltrim(" Hello")Returns "Hello".
ltrim("007", "0")Returns "7".
ltrim("abcHello", "a..c")Returns "Hello". The a..c range removes a, b, and c.

The second argument is a list of characters, not a prefix. Each character is removed one by one from the left until a different one is found.

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

Parameters

ParameterDescription
stringRequired. The input string.
charactersOptional. Characters to strip. Defaults to space, tab, newline, carriage return, vertical tab, and the NUL byte. Use a..z style ranges for sets.

Return Value

Return ValueDescription
stringThe string with the matching characters removed from the start.

PHP Examples

Example 1 — Remove Leading Spaces

The everyday use: clean up extra spaces before text.

PHP
<?php
$text = "   Hello, CodeToFun";
$clean = ltrim($text);

echo "[" . $clean . "]", PHP_EOL;

How It Works

  1. Start with spaces. Three spaces come before the text.
  2. Call ltrim with one argument. It uses the default whitespace list.
  3. Spaces are removed. Stripping stops at the first H.
  4. Brackets show the edges. There is no space after the opening bracket.

Example 2 — Custom Characters and Ranges

Strip zeros from a number string, and use a range to strip a set of letters.

PHP
<?php
echo ltrim("007", "0"), PHP_EOL;
echo ltrim("abcHello", "a..c"), PHP_EOL;
echo ltrim("0000", "0") === "" ? "empty" : "not empty", PHP_EOL;

How It Works

  1. Strip a character. Leading zeros are removed from 007.
  2. Use a range. a..c covers a, b, and c.
  3. Everything can go. A string of only zeros becomes an empty string.
  4. Watch the result. An empty string is not the number 0.

Example 3 — Left Side Only

ltrim() never touches the right side. Trim a leading slash from a path.

PHP
<?php
$path = "/var/www";
echo ltrim($path, "/"), PHP_EOL;

$padded = "  Hi  ";
echo "[" . ltrim($padded) . "]", PHP_EOL;

How It Works

  1. Strip a slash. The leading / is removed from the path.
  2. Try padded text. It has spaces on both sides.
  3. Only the left is cleaned. Two spaces remain after Hi.
  4. Need both sides? Use trim() instead.

Frequently Asked Questions

ltrim() removes whitespace or a set of characters you choose from the beginning of a string and returns the result.
string ltrim(string $string, string $characters = " \n\r\t\v\x00"). The characters argument is optional.
No. The second argument is a character list. Any of those characters are removed one at a time. To remove a prefix word, use str_starts_with() with substr().
ltrim() cleans the left side, rtrim() cleans the right side, and trim() cleans both sides.
Yes. Use two dots, like "a..z" or "0..9", inside the character list to cover a whole range.

Next: md5()

md5() calculates the MD5 hash of a string.

md5() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful