PHP ltrim() Function: Removing Leading Characters from a String

This article explains the PHP ltrim() function, its parameters, default character list, return value, and provides multiple code examples demonstrating how to trim whitespace, custom characters, and ASCII control characters from the beginning of strings.

Laravel Tech Community
Laravel Tech Community
Laravel Tech Community
PHP ltrim() Function: Removing Leading Characters from a String

The ltrim() function removes whitespace characters (or other specified characters) from the beginning of a string in PHP.

Parameters str: The input string. character_mask (optional): A list of characters to strip; ranges can be specified with ...

Return value

The function returns the input string with the leftmost characters removed. If character_mask is omitted, the default characters removed are:

Space (ASCII 32)

Tab (ASCII 9)

Newline (ASCII 10)

Carriage return (ASCII 13)

NULL byte (ASCII 0)

Vertical tab (ASCII 11)

Example usage

<?php
$text = "\t\tThese are a few words :) ...  ";
$binary = "\x09Example string\x0A";
$hello = "Hello World";
var_dump($text, $binary, $hello);
$trimmed = ltrim($text);
var_dump($trimmed);
$trimmed = ltrim($text, " \t.");
var_dump($trimmed);
$clean = ltrim($binary, "\x00..\x1F");
var_dump($clean);
?>

The above script demonstrates:

Removing default whitespace from $text.

Specifying a custom mask (space, tab, period) to trim additional characters.

Removing ASCII control characters (0–31) from $binary.

Output

string(32) "    These are a few words :) ...  "
string(16) "    Example string
"
string(11) "Hello World"
string(30) "These are a few words :) ...  "
string(30) "These are a few words :) ...  "
string(7) "o World"
string(15) "Example string
"
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Backendphp-functionsltrimstring-manipulationtrimming
Laravel Tech Community
Written by

Laravel Tech Community

Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.