Library contents

PHP ltrim

Remove leading spaces while preserving spacing at the other edge.

A simple example

ltrim

  1. Start with " A "
  2. Do this Remove leading spaces
  3. Result A then two spaces
Only the spaces before A disappear.
lesson.php
<?php
$value = "  A  ";
echo ("[" . ltrim($value) . "]"), PHP_EOL;
Output
[A  ]

Syntax

PHP syntax
"[" . ltrim($value) . "]"
Run these examples in PHP

Save one example at a time as lesson.php. Use PHP 8.2 or later.

php lesson.php

Align a pasted seat label

A user pastes a multi-line block from a formatted document into a plain-text field. Remove the common outer margin while preserving meaningful differences between the lines inside it.

From the program$value = " A-17 "
Result[A-17 ]
lesson.php
<?php
$value = "  A-17 ";
echo ("[" . ltrim($value) . "]"), PHP_EOL;
Output
[A-17 ]

The leading spaces vanish and the reserved space after 17 remains.

Advertisements

Clean a copied delivery field

A user pastes a name or code from another application. Remove the unwanted outer padding while preserving the spaces and letters that belong to the actual value.

From the program$value = "\tNorth Road"
Result[North Road]
lesson.php
<?php
$value = "\tNorth Road";
echo ("[" . ltrim($value) . "]"), PHP_EOL;
Output
[North Road]

The tab at the beginning is removed.

Clean a course import

A course name arrives padded on both sides. Remove only the leading spaces. Brackets make any spaces left at the end visible.

Given' Art '
Worked result[Art ]

What to watch for

The brackets in the output make significant trailing spaces visible.

Documentation for ltrim