Reputation: 61
In the below code $val whatever value you enter in the text box it is taking a space for example $val="6 " or $val="abc " Below is my simple code
foreach($_SESSION['cart'][$i] as $key => $val)
{
if($key=="company_name")
{
$company_name_session=$val;
}
elseif($key=="product_name")
{
$product_name_session=$val;
}
else if($key=="unit")
{
$unit_session=$val;
}
else if($key=="packing_size")
{
$packing_size_session=$val;
}
else if($key=="qty")
{
$qty_session=$val;
}
else if($key=="price")
{
$price_session=$val;
}
}
My Question is how to remove this space while declaring a variable in php .
Upvotes: 0
Views: 648
Reputation: 827
trim() should do it, if you're interested in removing the spaces around the text.
$val = trim($val);
You could also use the rtrim(), which trims the end.
Or if you wanted to remove all spaces in the variables, you could use str_replace()
$val = str_replace(' ', '', $val);
Edit:
And as Nathaniel mentioned, you can also use preg_replace()
, if you want to use regular expressions for more complex rules.
Upvotes: 1