PHP 8.5 ltrim() Function With Example

In PHP 8.5, the ltrim() function remains fully supported and works the same way it has in recent versions: it strips whitespace (or other specified characters) from the beginning (left side) of a string.

Function Signature

ltrim(string $string, string $characters = " vx00"): string

Parameters

→$string: The input string you want to trim.
→$characters (optional): A list of specific characters to remove. You can also use .. to specify a range (e.g., "x00..x1F" for control characters). If omitted, it removes standard whitespace like spaces, tabs, newlines, and NUL bytes.

Example

<?php
$text = "   Hello World!";
$trimmed = ltrim($text);

echo $trimmed; 
// Output: "Hello World!"

// Using the optional character mask
$path = "/var/www/html";
$cleanPath = ltrim($path, "/");

echo $cleanPath; 
// Output: "var/www/html"
?>

Summary

Use ltrim() when you specifically need to clean up the left side of a string. For removing characters from both ends, use trim(). For the right side only, use rtrim().

3 Views