-1

I made Cron job using amazon affiliate api and getting price for perrticular product but its not what i want. Its probably super easy but i am new to coding and cant fegure it out

responce from amazon api is INR 1,895.00 and i need 1895 as final result

    $price = "INR 1,895.00";

    $price = substr($price,4,10);

    //removes "INR "

    echo $price = (int)$price;

   // converts to integer 

The value of $price i get 1 due to "," and i m not able to solve it

Oleg Nurutdinov
  • 617
  • 4
  • 17
ABAD SOLO
  • 11
  • 4

1 Answers1

1

PHP has a built in filter to support this sort of thing

$price = "INR 1,895.00";
$price=( (float) filter_var( $price, FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION ) ); //outputs 1895

This solution also supports values like 'INR 19.95', resulting in '19.95'

Dimi
  • 1,255
  • 11
  • 20
  • This *may* go wonky with other national currency formats where the point and comma are reversed, e.g. `EUR 1.895,00` in NL – CD001 Jan 17 '19 at 15:14
  • @CD001 True, however, in this case data comes out of amazon api, which always has the same notation. – Dimi Jan 17 '19 at 15:34