Backend Development 3 min read

PHP ltrim() Function: Removing Leading Characters from Strings

The article explains PHP's ltrim() function, its syntax, parameters, default characters removed, and provides example code demonstrating how to strip whitespace or custom characters from the start of a string, including removing ASCII control characters.

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

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

Syntax: string ltrim(string $str [, string $character_mask])

Parameters

$str : the input string.

$character_mask (optional): a list of characters to strip; ranges can be defined with .. .

Return value : the trimmed string.

If $character_mask is omitted, ltrim() removes the following characters by default: space ( " " ), tab ( "\t" ), newline ( "\n" ), carriage return ( "\r" ), NUL byte ( "\0" ), and vertical tab ( "\x0B" ).

Example usage :

The first var_dump shows the original strings; after calling ltrim($text) the leading tabs are removed. Using a custom mask removes spaces, tabs, and periods. The final example demonstrates stripping ASCII control characters (0–31) from $binary .

BackendPHPfunctionsstring-manipulationltrim
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

login 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.