PHP regex捕获货币和数字一起


PHP regex to catch currency and number together

我是新手。

从句子中获取数字有一个问题。我试着去找,但不幸的是什么也没找到。

例如

:usd5 for potatoes. ->我想用php捕获"usd5"

我该怎么做?

试试这个

$str = 'usd5 for potatoes.';
$regex = '/(usd|gbp)'d/';
preg_match($regex, $str, $match);
var_dump($match);

这个网站提供了一个很好的正则表达式介绍

http://www.regular-expressions.info/tutorial.html

试试这个。U将得到一个数组

<?php
$str = 'usd5 for potatoes';
preg_match('/(?P<name>'w+)(?P<digit>'d+)/', $str, $matches);
print_r($matches);
?>

u将从$matches['name']获取货币,从$matches['digit']获取值