从货币中提取数字$ 100

时间:2018-10-20 19:33:35

标签: php

如何从Traceback (most recent call last): File "C:\Users\Bryan\Anaconda3\Scripts\jupyter-notebook-script.py", line 6, in <module> from notebook.notebookapp import main File "C:\Users\Bryan\Anaconda3\lib\site-packages\notebook\notebookapp.py", line 46, in <module> from zmq.eventloop import ioloop File "C:\Users\Bryan\Anaconda3\lib\site-packages\zmq\__init__.py", line 47, in <module> from zmq import backend File "C:\Users\Bryan\Anaconda3\lib\site-packages\zmq\backend\__init__.py", line 40, in <module> reraise(*exc_info) File "C:\Users\Bryan\Anaconda3\lib\site-packages\zmq\utils\sixcerpt.py", line 34, in reraise raise value File "C:\Users\Bryan\Anaconda3\lib\site-packages\zmq\backend\__init__.py", line 27, in <module> _ns = select_backend(first) File "C:\Users\Bryan\Anaconda3\lib\site-packages\zmq\backend\select.py", line 27, in select_backend mod = __import__(name, fromlist=public_api) File "C:\Users\Bryan\Anaconda3\lib\site-packages\zmq\backend\cython\__init__.py", line 6, in <module> from . import (constants, error, message, context, ImportError: DLL load failed: The specified module could not be found. 提取唯一数字

这不起作用currency $100? matchs [1]为空

我没有其他方法可以从字符串中提取数字

3 个答案:

答案 0 :(得分:4)

最好使用NumberFormatter::parseCurrency

<?php
$formatter = new NumberFormatter('en_US', NumberFormatter::CURRENCY);
$amount = '$100';
echo $formatter->parseCurrency($amount, $curr);

Demo here

这还将允许通常属于某个金额的字符(浮点数和逗号),并将其转换为适当的浮点值。

答案 1 :(得分:2)

您不需要preg_match_all,只需要普通的preg_match。
您的模式表明有多个数字吗?

这有效,但是不确定您的字符串是什么样的。您没有在您的问题中包括该内容。 它只匹配数字。

$price = "$100";
preg_match('/([\d\.]+)/',$price,$match); 
var_dump($match); //100

https://3v4l.org/BE0Nj

答案 2 :(得分:0)

$str = "$100";
preg_match_all('!\d+!', $str, $matches);
print_r($matches);

Reference

Array ( 
   [0] => Array ( 
                 [0] => 100 
              )
     )

echo $matches[0][0];

输出:100