-4

我是编码新手。

从句子中获取数字存在问题。我试图找到,但不幸的是什么也没找到。

例如: usd5 for potatoes.-> 我想用 php 捕获“usd5”

我怎样才能做到这一点?

4

2 回答 2

1

试试这个

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

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

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

于 2013-01-03T09:58:55.243 回答
0

试试 this.u 会得到一个数组的结果

<?php

$str = 'usd5 for potatoes';

preg_match('/(?P<name>\w+)(?P<digit>\d+)/', $str, $matches);

print_r($matches);

?>

你将从 $matches['name'] 获得货币,从 $matches['digit'] 获得价值

于 2013-01-03T10:00:08.857 回答