How to remove whitespace from the beginning of a string in PHP

The ltrim() function is a built-in function in PHP which is used to remove whitespace from the beginning of a string.



string – This parameter is required. It specifies the string to be checked.

charlist – This parameter is optional. It specifies which characters are to be removed from the string. These functions will remove the following whitespace characters:

  • Space character (” “)
  • Tab character (“t”)
  • Vertical tab character (“v”)
  • Newline character (“n”)
  • Carriage return character (“r”)
  • Null-byte character (“x0B”)


$str = '      PHP Tutorials!';
echo strlen($str);        // Output: 20
echo '<br>';
$trim_str = ltrim($str);
echo strlen($trim_str); // Output: 14