2013-02-21 37 views
-1

一些正則表達式的幫助下,我有一個字符串:尋找在PHP

[COLOR=gray]A bunch of text.[/COLOR] 

而且我想編寫一個preg_replace"[COLOR=gray]""[/COLOR]"之間消除一切 - 如果可能的話,除去這些標籤的,這是很好,否則我可以做一個簡單的替換之後。

回答

2
$str = 'dfgdfg[COLOR=gray]A bunch of text.[/COLOR]dfgdfgdfgfg'; 

$str1 = preg_replace('/\[COLOR=gray\].*\[\/COLOR\]/',"",$str); 

echo $str1; 

OR

如果顏色不是總是灰色

$str = 'dfgdfg[COLOR=gray]A bunch of text.[/COLOR]dfgdfgdfgfg'; 

$str1 = preg_replace('/\[COLOR=\w+\].*\[\/COLOR\]/',"",$str); 

echo $str1; 
+0

這工作時,我嘗試的例子;但是,在我的現場版本中,它不起作用。這可能與[color]代碼之前有幾個回車符有關嗎? – 2013-02-21 11:00:00

+0

行,看後它發現我的字符串其實是: [COLOR = grey] * return * 文字。 * return * [/ COLOR]謝謝@Prasanth Bendra。 – 2013-02-21 11:10:00

+0

我結束了使用[\ S \ s]而不是。 – 2013-02-21 11:15:36