我需要一个正则表达式,如果在百分号后跟一个数字并将其解压缩,我会在字符串的开头搜索。
$string = "20% - some text";
preg_match('/^[0-9]+%$/',$string);
应该返回20%
答案 0 :(得分:2)
您应该使用$matches
参数:
$string = "20% - some text";
$matches = array();
if (preg_match('/^([0-9]+%)/', $string, $matches)) {
print_r($matches);
}