PHP / regex - 从字符串中获取特定数字
问题描述:
Need quick help with regexp, to search a string for
blah blah blah price 4.000 blah blah
and assign 4.000 or whatever number comes after price to a variable.
需要快速帮助regexp,搜索字符串 p>
blah blah blah price 4.000 blah blah
code> pre>
并将4.000或价格后的任何数字分配给变量。 p>
div>
答
preg_match('~price\s+(\S+)~', $input, $matches);
var_dump($matches);
答
$num = preg_replace('/price ([\d,\.]+)/', '\1', $string);
or
preg_match('/price ([\d,\.]+)/', $string, $matches);
$num = $matches[1];