PHP Tutorial PHP Advanced PHP Resources
Python Java C++ C C# PHP R SQL DS Algo InterviewQ

PHP - String ltrim() Function



The PHP String ltrim() function returns a string which is left side trimmed version of the specified string. It removes whitespaces or other characters from left side of the string.

Syntax

ltrim(string, characters)

Parameters

string Required. Specify the string to trim.
Characters Optional. Specify characters which need to be trimmed from the string. Without this parameter, the function will strip following characters:
  • " " - an ordinary space.
  • "\t" - a tab.
  • "\n" - a new line (line feed).
  • "\r" - a carriage return.
  • "\0" - the NULL-byte.
  • "\v" - a vertical tab.

Return Value

Returns the left side trimmed version of the specified string.

Example:

In the example below, ltrim() function returns the left side trimmed version of the given string.

<?php
$MyString = "    Hello World!.    ";
$NewString = ltrim($MyString);

echo $NewString."\n";
?>

The output of the above code will be:

Hello World!.    

Example:

We can also specify the characters which need to be trimmed from the string as shown in the example below.

<?php
$MyString = ",*#Hello World!#@!";
$NewString = ltrim($MyString, ",*#@!");

echo $NewString."\n";
?>

The output of the above code will be:

Hello World!#@!

❮ PHP String functions

5