使用preg_match无法正常提取ID

时间:2015-03-02 20:01:31

标签: php preg-match

我正在尝试使用preg_math从某些内容中提取ID号...

$content = "[gravityform id="837" name="Test Form" title="false" description="false"]";
preg_match('/[gravityform\s*id="\s*([0-9]+)/i', $content, $id);
echo $id[1];

这对我不起作用,并且返回错误..

preg_match(): Compilation failed: unmatched parentheses 

我哪里出错了,preg_match是最好的方法吗?

1 个答案:

答案 0 :(得分:1)

在$ content中使用单引号以避免编译错误并转义正则表达式开头的[

$content = '[gravityform id="837" name="Test Form" title="false" description="false"]';
preg_match('/\[gravityform\s*id="\s*([0-9]+)/i', $content, $id);
echo $id[1];

输出:

837