-1

The title says it all. I need to remove any existing space before reaching the first non space character.

For example, consider " Hello World" a string that we want to operate on. I need to convert it to "Hello World". How would I do that in PHP?

Vahid Amiri
  • 10,769
  • 13
  • 68
  • 113
  • 1
    You can use [trim](http://php.net/trim) for removing spaces from both sides of a string. Or [ltrim](http://php.net/ltrim) for removing only from the left side. – Andrei Jan 20 '16 at 16:09
  • Use [`ltrim()`](http://php.net/manual/en/function.ltrim.php) function. – Rajdeep Paul Jan 20 '16 at 16:09
  • @Andrew, Rajdeep, if you provide that as answer I'll accept it. – Vahid Amiri Jan 20 '16 at 16:11
  • I think http://stackoverflow.com/questions/1101322/trim-leading-white-space-with-php, is a better duplicate, the matched duplicate is for any whitespace, not specific to leading. – chris85 Jan 20 '16 at 16:14

1 Answers1

1

Use ltrim() function to remove whitespaces from the beginning of a string.

$str = "        Hello World";
$sanitized_string = ltrim($str);
echo $sanitized_string;  // Hello World
Rajdeep Paul
  • 16,887
  • 3
  • 18
  • 37