7

I want to parse a currency from a string in PHP, I've had a look at number formatter but haven't got PHP 5.3 or the ability to add extensions.

The currency will only exist once per string, and will be prefixed with a currency symbol, in my case the pound sign £. The currency may be in one of the following formats:

£0.90
£100
£100.10
£1000

What would be the best method of achieving this?

Edit

Here is an example string:

Paid a bill £153.93

I want to get the currency value into an variable.

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
Ben Everard
  • 13,652
  • 14
  • 67
  • 96

6 Answers6

10
(float)substr($input, strpos($input, "£")+1);

This will get you the following results:

float(0.9)
float(100)
float(100.1)
float(1000)
float(153.93)

EDIT: updated to reflect the change to question. this is assuming all strings are like the one you gave as an example.

Alec Gorge
  • 17,110
  • 10
  • 59
  • 71
  • 2
    Also, a suggestion, if he's gonna be working with or storing the money values, he should work with/store them as integers, by multiplying by 100 of course and casting it to int (just in case), essentially working in number of pennies (or whatever the base unit is in British currency, pence?), then convert them back to floats in pounds for display, in order to get around the potential inaccuracy of float calculations. – Phoenix Feb 20 '11 at 21:17
  • 1
    Right, this is looking good but I think I have an issue with the encoding £ sign... – Ben Everard Feb 20 '11 at 21:19
  • Doesn't work probably an encoding issue because + 2 fixes it. – malhal Dec 15 '15 at 21:35
7

I've got another answer. Might be a touch faster than using strpos, and would be better if there was any possibility of white space in the input.

$input = "£250.75";
$output = floatval(ltrim($input,"£"));
echo $output;
250.75

You could also add other currencies to the char list in ltrim:

$output = floatval(ltrim($input,"£$¢"));

This would strip $ or £ or ¢ from the left side of your number, as well as white space, which would break the solution above which uses strpos. Also, this would give the same result if the currency symbol was left off in some cases.

Sepster
  • 4,800
  • 20
  • 38
cmpreshn
  • 162
  • 2
  • 8
2
preg_match('/(?<=£)(?=[\d.]*\d)(\d*(?:\.\d*)?)/', $input, $matches);

will find a match within any of these:

  • £.10
  • £0.10
  • £100
  • £100.00

etc.

Ωmega
  • 42,614
  • 34
  • 134
  • 203
0

You could do it with a regular expression ($matches[1] will have your value):

preg_match('/£([0-9]+|[0-9]+\.?[0-9]{2})/', $text, $matches);
Dave Child
  • 7,573
  • 2
  • 25
  • 37
  • you most certainly don't need regex for this – Alec Gorge Feb 20 '11 at 20:39
  • As I understand it, he's looking to extract the number from a larger string (but it might appear in one of several forms within that string), for example taking 123.45 out of this comment containing £123.45. – Dave Child Feb 20 '11 at 20:40
0

Do it all, directly, with one native function call -- sscanf().

Consume one or more non-£ characters from the front of the array, but do not assign it to a reference variable or return it (that's what the * does). Capture the float expression string after the £. The captured string will be represented as the lone element in the return array and its data type will be float.

Code: (Demo)

$tests = [
    'Paid a bill £0.90',
    'Paid a bill £100',
    'Paid a bill £100.10',
    'Paid a bill £1000'
];
foreach ($tests as $test) {
    var_dump(sscanf($test, '%*[^£]£%f')[0]);
    echo "\n";
}

Output:

float(0.9)

float(100)

float(100.1)

float(1000)
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
-3
$price = (float) substr($input, 1, strlen($input) - 1);
codenamepenryn
  • 451
  • 1
  • 7
  • 18