-1

Okay, this is probably a pretty basic problem but I can't solve it.

I have a String like this one: ' This is a text' and now I wan't to remove the spaces infront of the first character of the string. I tried using preg_replace but I can only remove all the spaces using that function. Does anyone have an idea how I can remove just the spaces infront of my Text?

wlfkd
  • 11

2 Answers2

1

Use trim:

echo trim(' This is a text '); // 'This is a text'

If you want to preserve any spaces at the end of the string, use ltrim:

echo ltrim(' This is a text '); // 'This is a text '

Similarly, although not your question, you can preserve any spaces at the beginning of the string by using rtrim:

echo rtrim(' This is a text '); // ' This is a text'
Mooseman
  • 18,763
  • 14
  • 70
  • 93
  • If it it's *only* the left side you want, (for example, you wanted to keep spaces on the right side), use "left trim": $var = ltrim($string); – Kevin_Kinsey Dec 01 '14 at 16:40
0

Use the trim() function:

$str = trim($str)

See the PHP manual: http://php.net/manual/en/function.trim.php

danmullen
  • 2,556
  • 3
  • 20
  • 28