[Snippet] Formatting a currency string

The Shopp ecommerce plugin for WordPress has a little annoying “feature” that returns the product price as a string pre-formatted for your region and currency.

I needed to do some maths on the string value, so some hacking was required to remove all the  non-alphanumeric-but-not-decimal-points.

function kb_strip_money($string) {
  return preg_replace("/[^0-9\.]/Uis", "", shopp('product','price','return=true'));
}

 

Or a generic non-Shopp option

function kb_strip_money($string) {
  return preg_replace("/[^0-9\.]/Uis", "", $string);
}

£45.50 or $35.89 would become 45.50 and 35.89 where you can then do maths on the value.

Author: Kieran Barnes

Kieran is a PHP developer with 15 years commercial experience. Specialist in WordPress, CakePHP, CubeCart and all things PHP.

Leave a Reply

Your email address will not be published. Required fields are marked *