Reputation: 27
I seem to be having trouble removing the plus sign from the end of a number.
I have a file which contains stock levels. When the stock is over 20 they use 20+.
This does not seem to work.
case 'MTA' :
if (isset($line[0])) {
$stock = ($line[15] == '20+') ? 20 : $line[15];
}
$rows[] = array('sku' => $line[0], 'stock' => $stock);
}
break;
I've also tried the following lines with no luck:
if ($line[15] == '20+') {
$line[15] = (int)20;
$stock = (int)str_replace('20+', '20', $line[15]);
$stock = str_replace('+', '', $line[15]);
Any suggestions would be greatly appreciated!
Upvotes: 1
Views: 64
Reputation: 1191
You can use rtrim
,substr_replace
,substr
As per your example, substr_replace
it can be written as follows
$line[15]= '20+';
$stock=substr_replace($line[15] ,"",-1);
echo $stock;
Using substr
$line[15]= '20+';
$stock=substr($line[15], 0, -1);
echo $stock;
Using rtrim
$line[15]= '20+';
$stock=rtrim($line[15], "+");
echo $stock;
Upvotes: 2