Trim should do exactly what you're looking for without needing
ereg_replace, etc.

Trim trims all whitespace before the first non-whitespace character and
after the last non-whitespace character. I've included an example and
its output below.

File: test.php
<?php

header("Content-type: text/plain");

$test_str = "      This is my test str      ";
echo "'$test_str'\n\n";

$test_str = trim($test_str);
echo "'$test_str'";

?>

Output: test.php
'      This is my test str      '

'This is my test str'

Note: The single quotes are included in the output to show where the
spaces are.

Hope this helps!

--Toby

jaskirat wrote:
> 
> Since this thread is running I would like to know the gurus' opinons on the
> similar following situation
> 
> Suppose there is a variable like this
> 
> $var = "This is a variable          ";
> 
> Now I want to strip off white spaces on the end without losing them in the
> middle of the sentence ..
> 
> I had figured that there is a no direct way of doing it and some thing like
> this
> works
> $var = trim (ereg_replace("  ","",$var));
> 
> the first argument to ereg_replace is  "two spaces" so that single spaces
> between the words are not removed
> then trim to remove any space on the end which may still remain.
> 
> But I feel its still not a fool proof solution because there may be two
> spaces in between words some where also
> which will then be clubbed together.
> 
> Any body has a better idea.
> 
> Jaskirat
> 
> At 09:51 PM 2/16/01 -0500, you wrote:
> >> Trim() will in fact only strip whitespace from the beginning or end of
> >> the string; if he has \n's anywhere in the middle of the string, trim
> >> will do perform the desired operation.
> >
> >CORRECTION : "trim will not perform the desired operation"
> >
> ><snip>
> >
> >--
> >PHP General Mailing List (http://www.php.net/)
> >To unsubscribe, e-mail: [EMAIL PROTECTED]
> >For additional commands, e-mail: [EMAIL PROTECTED]
> >To contact the list administrators, e-mail: [EMAIL PROTECTED]
> >

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]

Reply via email to