Remove extra whitespace from the beginning PHP?


If you want to remove extra whitespace from the beginning, use ltrim() in PHP. Let’s say the following is our variable −

$value="  Hello, welcome to PHP Tutorial  ";

We want the output with no whitespace on the left −

Hello, welcome to PHP Tutorial

Example

 Live Demo

<!DOCTYPE html>
<html>
<body>
<?php
   $value="  Hello, welcome to PHP Tutorial  ";
   print_r( "The actual value is=".$value."<br>");
   echo "The actual length is=",strlen($value)."<br>";
   $modifiedValue=ltrim($value);
   echo "After removing extra whitespace from beginning=",strlen($modifiedValue)."<br>";
   echo "The result is=".$modifiedValue;
?>
</body>
</html>

Output

This will produce the following output

The actual value is= Hello, welcome to PHP Tutorial
The actual length is=34
After removing extra whitespace from beginning=32
The result is=Hello, welcome to PHP Tutorial

Updated on: 13-Oct-2020

109 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements