2012-04-17 47 views
0

考慮以下幾點:如何從字符串中刪除標籤?

$string = "A string with {LABELS} and {more|232} {lbls} and some other stuff"; 
echo str_replace('/(\{.*?\})/', '', $string); 

我試圖去除所有標籤(標籤是{brackets}之間的任何文本)。預期產量爲:

A string with and and some other stuff 

但是我所得到的是原始字符串:

A string with {LABELS} and {more|232} {lbls} and some other stuff 

我在做什麼錯?

+1

你的問題中的2個字符串是一樣的!?!?! – ManseUK 2012-04-17 15:28:00

+0

您的預期和實際結果看起來一樣。 – 2012-04-17 15:28:06

+0

我看到預期和原始之間沒有區別。 – elmo 2012-04-17 15:28:08

回答

2

您將需要使用preg_replace代替:

$string = "A string with {LABELS} and {more|232} {lbls} and some other stuff"; 
echo preg_replace('/\{.*?\}/', '', $string); 
0

嘗試:

echo preg_replace('/\{.*?\}/', '', $string); 
+0

這將匹配字符串中第一個「{」和最後一個「}」之間的所有內容。 – kapa 2012-04-17 15:32:04

+0

謝謝,我忘記了問號 – 2012-04-17 15:32:49

0
preg_replace('/\{.*?\}/','',$str) 
0

一定要使用的preg_replace,但你需要一個稍微不同的正則表達式來篩選出空間,並確保你匹配的大括號正確

$string = "A string with {LABELS} and {more|232} {lbls} and some other stuff"; 
echo preg_replace('/\s*\{[^}]*\}/', '', $string); 

得出:用一個字符串,以及一些其他的東西