Page 1 of 1

Removing spaces from the start and end of a string

Posted: Thu Jun 10, 2004 3:56 am
by starsol
I'm having a few problems trying to remove spaces from the beginning and end of a sting (if either or both first and last character is a space). Any spaces in the middle of the string are ok, so str_replace isn't the solution.

What I have so far is:

Code: Select all

$length = strlen($string);
$explode = explode("", $string);
if ($explodeї0] == " "){
	array_shift($explode);
}
if ($explodeї$length]) == " "){
	array_pop($explode);
}
$string = implode("", $explode);
This of course gives parse errors at $explode[length]. Can anyone advise me how I can check if the last value in the exploded array is a space?

Posted: Thu Jun 10, 2004 4:48 am
by patrikG
php-manual: ltrim() and rtrim()

Posted: Thu Jun 10, 2004 5:13 am
by feyd
also [php_man]trim[/php_man]() will remove leading and trailing spaces at the same time.

Posted: Thu Jun 10, 2004 6:20 am
by starsol
Cheers B).