从字符串解析货币 [英] Parse currency from string

查看:89
本文介绍了从字符串解析货币的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从PHP中的字符串中解析货币,所以我看过数字格式化器但没有PHP 5.3或添加扩展的功能.

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?

修改

这是一个示例字符串:

Paid a bill £153.93

我想将货币值转换为变量.

I want to get the currency value into an variable.

推荐答案

(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.

这篇关于从字符串解析货币的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆