xl_cheese
xl_cheese

Reputation: 27

Strip plus sign from end of number

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

Answers (3)

affaz
affaz

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

B. Desai
B. Desai

Reputation: 16436

try this:

$line[15]= '20+';
$stock = intval($line[15]);

Upvotes: 0

pooya_sabramooz
pooya_sabramooz

Reputation: 168

use this:

$stock = rtrim($line[15],"+");

Upvotes: 2

Related Questions