3

I have a php string which contains a currency. In this case it is a € but to make it future proof I would also like to replace other currency's. How can I filter this currency symbol out?

I tried preg replace but don't know the key to filter currency notations out, also I saw a couple of posts in which they did it entirely different, but couldn't manage to get them to work.


Tried your suggestions guys, still doesn't work. If I add a "," to the suggestions below in this post it does remove the , from the price so it does seem to do something. But the € sign remains.

The price is coming from a woocommerce array, and right before I want to remove the € sign I use a "strip_tags" command. Maybe that has to do something with it. If is use a if else statement to see if it is a string it echo's true, so it isn't a float or anything.

Spindle
  • 190
  • 3
  • 9

2 Answers2

7

If you want to do it quick and easy, just do a string replacement:

$symbols = array('$', '€', '£');
$unformatted = str_replace($symbols, '', $price_string);
giorgio
  • 10,111
  • 2
  • 28
  • 41
1

Why not write a simple str_replace() ?

<?php
function strip_currency($str)
{
    return str_replace(array('$','€','£','pуб'),'',$str); // You can add-up currency symbols here
}
$yourstr='I have 10$ with me !';
echo $yourstr = strip_currency($yourstr); //"prints" I have 10 with me !

Demo

Shankar Narayana Damodaran
  • 68,075
  • 43
  • 96
  • 126