开发者

How to replace "{* foo *}" with nothing?

I want to replace all text what start with "{" and end with "}" including those "{" and "}". Text between them may be anything. =( I guess I need to use regex's there.

Let's imagine that $this->output is str开发者_如何转开发ing that I want to replace. So I got this far... It don't work by the way. =(

$this->output = preg_replace( '/{*(.*)*}/', $this->output, $this->output );


You need to escape the *:

'/{\*(.*)\*}/'

Otherwise * is interpreted as quantifier. Furthermore, to have .* not to match as much as possible, make the * quantifier reluctant:

'/{\*(.*?)\*}/'


Going by the question text which asks to match {foo}, {[^}]*} which will match anything in { / }'s.

Going by the subject which asks to match {*foo*}, {\*.*?\*} which will match anything in {* / *}'s. To match only {*foo*} in {* foo {*foo*}, use {\*((?!{\*).)*?\*}.


What you're looking for is slightly unclear so I'll address both cases.

To replace the foo in {foo} use the regex:

/{\*([^}])*\*}/

To replace the foo in {foo} use the regex:

/{([^}])*}/
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜