从变量中删除文本字符,留下数字和逗号?
我需要能够从一些变量中删除字符,只留下数字和 £
符号(当然除非 mysql 可以在自身中添加它?)以及任何 .
> 分隔符。
因此,如果变量 $price_data contains
Now £193.95
我如何最终得到
193.95
?
我需要完成此操作的原因是我需要能够将数据以十进制形式插入到字段中,并且能够将其从最便宜到最昂贵进行排列。
I need to be able to strip from characters from a few variables leaving only the numbers and the £
sign (unless of course mysql can add that in itself?) and also any .
separators.
so if the variable $price_data contains
Now £193.95
How do I end up with
193.95
?
Reason I need this done is I need to be able to insert the data into the field as decimal, and be able to arrange it from least to most expensive.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
根据输入数据,删除任何前导或尾随非数字可能会更可靠:
如果用数字括起来,则会留下点,并且可以与文字
£
以及一起使用>£
转义,并删除任何尾随垃圾。Depending on the input data it might be more reliable to remove any leading or trailing non-numbers:
This leaves in the dot if enclosed by numbers, and would work with a literal
£
as well as the£
escape, and removes any trailing garbage.试试这个:
这是一个演示:http://codepad.org/az87F5wA
try this:
here is a demo: http://codepad.org/az87F5wA
财务数据应存储为整数,而不是由于浮动数据类型的精度损失而存储为浮点数:http: //en.wikipedia.org/wiki/Precision_(computer_science)
因此,您应该使用以下代码将其作为整数(代表美分)存储在 MySQL 中以仅提取数字:
另外,当您对整数类型执行除法,因为您可能会“损失美分”。
华泰
Financial data should be stored as integers, not floats due to loss of precision with floating datatypes: http://en.wikipedia.org/wiki/Precision_(computer_science)
Therefore, you should store it in MySQL as an integer (representing cents) using the following code to extract only the digits:
Also, be careful when you perform division on integer types because you can "lose cents".
HTH