preg_match特殊字符

时间:2014-01-04 14:34:46

标签: php regex preg-match

我正在尝试在我的PHP代码中创建一个preg_match,但我似乎无法让它工作。

我有这3个标签

{$success url='http://www.domain.localhost/form-success'}
{$error url='http://www.domain.localhost/form-error'}
{$button title='send form'}

如何让preg_match接受我正在尝试做的事情?

我正在尝试将{$button(*)}带到我的匹配路径,并且我想对其他2个{$success(*)}{$error(*)}

做同样的事情

现在我觉得它应该是这样的

preg_match("/\{\$button(.+?)}/s", $content, $match);

但它仍然不起作用,所以我希望其他人可以帮助我。

3 个答案:

答案 0 :(得分:1)

您需要逃避结束},并且需要使用preg_match_all来匹配所有行。

试试这个正则表达式:

preg_match('/\{\$(?:success|error|button)\s+([^}]+)\}/i', $content, $match );

工作演示:http://ideone.com/fKq5L2

输出:

Array
(
    [0] => {$success url='http://www.domain.localhost/form-success'}
    [1] => {$error url='http://www.domain.localhost/form-error'}
    [2] => {$button title='send form'}
)

答案 1 :(得分:1)

我认为检索“button”标记的正确正则表达式应为:\{\$button([^\}]*)\}

您可以在http://regexpal.com/

上尝试表达

所以使用php:

preg_match("/\{\$button([^\}]*)\}/s", $content, $match );

答案 2 :(得分:1)

\$已经在PHP字符串中表示“literal $”,因此当放入正则表达式时,它只是意味着“字符串结束”。

请尝试\\\$