2011-11-19 76 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