使用php提取正则表达式

时间:2012-09-09 00:10:17

标签: php regex

我需要一个正则表达式,如果在百分号后跟一个数字并将其解压缩,我会在字符串的开头搜索。

$string = "20% - some text";

preg_match('/^[0-9]+%$/',$string);

应该返回20%

1 个答案:

答案 0 :(得分:2)

您应该使用$matches参数:

$string = "20% - some text";
$matches = array();
if (preg_match('/^([0-9]+%)/', $string, $matches)) {
     print_r($matches);
}