Click to See Complete Forum and Search --> : Truncating variables?


hammerslane
11-18-2003, 05:04 AM
Hi... a simple question i'm sure, but i can't find a function which will trim variables down and, if necessary, eliminate spaces. I think that strto_[something]_ might be half way there?
summary: i want to take $variable (which has a value of "i am a variable") and make it only 3 characters long, excluding spaces, so turn it into "IAM".

regards

hammerslane
11-18-2003, 06:00 AM
sorry to double post... but i have now found the solution.
substr does it quite nicely.
however... now I am trying to truncate all spaces from a string. There are variables which remove white space from the beginning and end of strings... but i can't find one that does it completely... any help?
i'll be back in an hour...
thanks.

pyro
11-18-2003, 07:27 AM
You can simply use str_replace (http://us4.php.net/manual/en/function.str-replace.php)

$var = str_replace(" ", "", $var);

hammerslane
11-21-2003, 04:03 AM
thanks pyro...
can i use several str_replace's for one variable? for example...

$variable = str_replace(" ", "", $variable);
$variable = str_replace("the", "", $variable);
$variable = str_replace(",", "", $variable);

it won't get rid of 'the' or any commas when i do that.

regards

pyro
11-21-2003, 07:13 AM
Yes, you can. You must have some other problem, as this works fine:

<?PHP

$variable = "Testing the str_replace() out, ok?";

$variable = str_replace(" ", "", $variable);
$variable = str_replace("the", "", $variable);
$variable = str_replace(",", "", $variable);

echo $variable;

?>