-1

I'm a newbie with PCRE in PHP. I'm trying to make a very basic shortcode function that could make something with a format like this one: {somealphanumericthing}

In essence I need a preg_match_all() that could find in my post these type of occurrences. I tried something like this:

$shortcode = preg_match_all('/^\b\{[a-zA-Z0-9_]+(\}\b)$/', $body, $found);
    var_dump($shortcode);
    if($shortcode==1) {
        for($i=0;$i<count($found);$i++) {
            print_r($found);
                        //do something nice
        }
    }

But unfortunately it's not working: I get int 0 to the test string {test}

4

1 回答 1

1

关于正则表达式的一些事情:

  • 您不需要线锚,因为您正在搜索更大的字符串。
  • 没有必要捕捉关闭}
  • 优化,使用字符类\w

浓缩:

/\b\{[a-zA-Z0-9_]+\}\b/
于 2013-07-30T13:58:27.837 回答