2012-09-09 65 views
1

我需要一个正则表达式,如果有一个数字后跟一个百分号并将其提取出来,那么我会在字符串的开头搜索什么。用php提取正则表达式

$string = "20% - some text"; 

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

应该回到20%

回答

2

您应该使用$matches参数:

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

非常感谢您的帮助 –