2011-11-19 77 views
0

我有这样的字符串“{\ i1}你很高兴?{\ i0}” ,我想删除所有“{...}”,只是文字“你很高兴? “离开了。PHP Preg_replace字符串

我试过用一些正则表达式模式,但我没有得到它的工作。

我的一个尝试的:

text = preg_replace("/{.*}/", "\\0", $text); 

回答

3

您可以使用此更换{}

$result = preg_replace('/\{[^}]*\}/', '', $subject);

说明

" 
\{  # Match the character “{” literally 
[^}] # Match any character that is NOT a “}” 
    *  # Between zero and unlimited times, as many times as possible, giving back as needed (greedy) 
\}  # Match the character “}” literally 
" 
+0

谢谢!很棒! – Stefan

1

网络之间的所有文字首先你应该让比赛不太成立。在*之后应用?

在这个例子中,开头{不需要转义,但我仍然会这么做。

然后您使用\0作为替换模式。无论正则表达式匹配如何,将重新插入。所以最终没有任何东西会被删除 - 我听说的不是你想要的。

$text = preg_replace("/\{.*?}/", "", $text); 
+0

也谢谢你!很好的解释。 – Stefan