Friday, October 10, 2025
HomeLanguagesPHP ltrim() Function

PHP ltrim() Function

The ltrim() function is a built-in function in PHP which removes whitespaces or other characters (if specified) from the left side of a string.

Syntax:

ltrim( $string, $charlist )

Parameters: The function ltrim() accepts two parameters as shown in the above syntax. Out of these two parameters, one is mandatory while the other one is optional. They are discussed in details below:

  • $string : This mandatory parameter specifies the string to be checked.
  • $charlist : This optional parameter specifies which characters are to be removed from the string.In case, this parameter is not provided, the following characters are removed :
    • “\0” – NULL
    • “\t” – tab
    • “\n” – new line
    • “\x0B” – vertical tab
    • “\r” – carriage return
    • ” “ – ordinary white space

Return Value: Returns the modified string.

Examples:

Input : $string = "            Geeks for Geeks"
Output : Geeks for Geeks

Input : $string = "!!! (( !!)) Geeks for Geeks", $charlist = "! ()"
Output : Geeks for Geeks

Below programs illustrate the ltrim() function :

Program 1 : This program shows the use of ltrim() function without any specified list of characters to be removed.




<?php
  
$string = "            Geeks for Geeks";
  
echo "Contribute to ".ltrim($string);
  
?>


Output:

Contribute to Geeks for Geeks

Program 2: This program shows the use of ltrim() function with a specified list of characters to be removed.




<?php
  
$string = "!!! (( !!)) Geeks for Geeks";
  
// The characters '!', '(', ')', ' ' have 
// been specified to remove from the
// beginning of the string
echo ltrim($string, "! ()");
  
?>


Output:

Geeks for Geeks

Reference:
http://php.net/manual/en/function.ltrim.php

RELATED ARTICLES

Most Popular

Dominic
32349 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6717 POSTS0 COMMENTS
Nicole Veronica
11880 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11941 POSTS0 COMMENTS
Shaida Kate Naidoo
6838 POSTS0 COMMENTS
Ted Musemwa
7097 POSTS0 COMMENTS
Thapelo Manthata
6792 POSTS0 COMMENTS
Umr Jansen
6792 POSTS0 COMMENTS