3

I want to get price value without currency symbol in Product Page for OpenCart. I have use following code for that. but, it is not work perfectly.

I have found and used following code. in .tpl file.

<?php
 $pricenocurrency = $price;
 $pricenocurrency  = preg_replace( '/\D/', '', $pricenocurrency  );
 echo $pricenocurrency ;
?>

So, I get following result. but, I want to not remove dot(.) from price.

Default Price = 86.02€

I Got = 8602

I want to = 86.02

HDP
  • 3,003
  • 2
  • 28
  • 50
  • Possible duplicate of [RegEx - How to Extract Price?](http://stackoverflow.com/questions/2430696/regex-how-to-extract-price) – Thamilhan May 22 '16 at 11:36
  • @HarnishDesign Glad to help. Good luck! – splash58 May 22 '16 at 11:56
  • Possible duplicate of [Decimal or numeric values in regular expression validation](http://stackoverflow.com/questions/2811031/decimal-or-numeric-values-in-regular-expression-validation) – billynoah May 23 '16 at 14:21

3 Answers3

2

add point to preg_replace condition

 $pricenocurrency  = preg_replace( '/[^.\d]/', '', $pricenocurrency  );

if text around price could contain digit, make it a little complicated to save only points after digit

(?<!\d)\.|[^\d]
splash58
  • 25,216
  • 3
  • 19
  • 31
2

The above solution works fine, but what if there are multiple currencies on your store.I would recommend to use default opencart functionality. Just do this,

In your controller do the following.

$data['price_without_symbol'] = $this->currency->format($amount,$currency_code,$currency_value,false);

Will result in price without currency symbol. And use it in your .tpl file. Recommended if your store supports multi-currency. Rest for knowledge. :)

Vidhyut Pandya
  • 1,481
  • 1
  • 12
  • 23
-1

You can use this also

<?php
    $pricenocurrency = $price;
    $pricenocurrency  = preg_replace( '/[^0-9.]/', '', $pricenocurrency  );
    echo $pricenocurrency ;
?>
Ahmed Al Bermawy
  • 1,678
  • 2
  • 32
  • 32