简体   繁体   English

PHP preg_match_all:正则表达式保持性

[英]PHP preg_match_all: regular expression assistence

I have a question about expression. 我有一个关于表达的问题。 I've looked the forum through but I'm still stuck with the same problem. 我已经浏览了整个论坛,但仍然遇到相同的问题。 I found a sample on the php.net page which has an expression similar to what I'm looking for but still no success. 我在php.net页面上找到了一个示例,该示例的表达式与我正在寻找的表达式相似,但仍然没有成功。 What I'm trying to achieve is, to get words which are between {}. 我想要达到的目的是获得{}之间的单词。 IE: IE浏览器:

$html = '<h1>Hello {NAME_P}, today is {TODAY}.</h1>';

The expression I'm using is preg_match_all("/{[^}]*}/", $html, $hooks); 我正在使用的表达式是preg_match_all("/{[^}]*}/", $html, $hooks); but it returns 0 = Array . 但它返回0 = Array

I would like to have the strings NAME_P and TODAY as result. 我希望将字符串NAME_P和TODAY作为结果。

Thanks in advance! 提前致谢!

You need to capture them I believe. 我相信您需要捕获它们。 To do that, use brackets as follows: 为此,请使用方括号,如下所示:

preg_match_all("/{([^}]*)}/", $html, $hooks);
                  ^     ^

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM