55 of 410 menu

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

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

Example

Let's remove the whitespace from the left:

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

Code execution result:

'abcde '

Example

Let's remove the slashes from the left:

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

Code execution result:

'abcde/'

Example

Let's remove slashes and dots from the left:

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

Code execution result:

'abcde'

Example

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

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

Code execution result:

'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 echo ltrim('abcde', 'a..d'); ?>

Code execution result:

'e'

See Also

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