The ltrim Function

The ltrim function removes whitespace from the beginning of a string. It can also remove other characters if they are specified as the second parameter.

Syntax

phpltrim(string $string, string $characters = " \t\n\r\0\x0B"): string

Example

Let's remove the whitespace from the left:

php<?php
  echo ltrim(' abcde ');
?>

Code execution result:

php'abcde '

Example

Let's remove the slashes from the left:

php<?php
  echo ltrim('/abcde/', '/');
?>

Code execution result:

php'abcde/'

Example

Let's remove slashes and dots from the left:

php<?php
  echo ltrim('/abcde', '/.');
?>

Code execution result:

php'abcde'

Example

The function removes any number of the specified characters if they are on the edge:

php<?php
  echo ltrim('../../abcde', '/.');
?>

Code execution result:

php'abcde'

Example

You can specify a range of characters using two dots '..'. For example, let's specify that we want to remove characters from 'a' to 'd':

php<?php
  echo ltrim('abcde', 'a..d');
?>

Code execution result:

php'e'

See Also

  • the trim function,
    which removes whitespace from all sides
  • the rtrim function,
    which removes whitespace from the right