Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

php
communitycreator

What is ltrim() in PHP?

Harris Amjad

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The ltrim() function removes the leading space characters or other specified characters from the left side of a string.

The image below shows a visual representation of the ltrim() function.

Visual representation of ltrim() function

Syntax

ltrim(string,charsToBeRemoved)

Parameters

The ltrim() function takes two parameters:

  • A string from which characters need to be removed.
  • The list of characters that are to be removed from the left side of the string (optional).

If the value of the charsToBeRemoved is not specified, then the following characters are removed from the left side of the string:

  • " " - white space
  • “\t” - tab
  • “\n” - new line
  • “\x0B” - vertical tab
  • “\r” - carriage return
  • “\0” - NULL

Return value

ltrim() removes the leading space characters or the other specified characters from the left side of a string.

Code

The example below shows how to use ltrim() in PHP.

<?php
#single character removed from string
echo("ltrim('!educative','!'): ");
echo(ltrim('!educative','!'));
echo("\n");
echo("ltrim('!()educative','!'): ");
echo(ltrim('!()educative','!'));
echo("\n");
#multiple characters removed from string
echo("ltrim('!,()educative','! , ()'): ");
echo(ltrim('!,()educative','! , ()'));
echo("\n");
#default ~ without the list of characters to be removed
echo("ltrim(' educative'): ");
echo(ltrim(' educative'));
echo("\n");
?>

RELATED TAGS

php
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring