2013-02-03 84 views
0

我想知道我怎麼能刪除一組括號及括號自己在PHP中的正則表達式之間的文本,該paretheses可能是不同的......例如:刪除內容用PHP

[爲myContent}

(爲myContent]

{爲myContent)

等等...

我曾嘗試:

preg_replace("/\{.*\}/", " ", $title); 
preg_replace("/\[.*\]/", " ", $title); 
preg_replace("/\(.*\)/", " ", $title); 

但這只是刪除括號一樣..和可以被嵌套至第二級是括號..

+0

難道這括號嵌套?例如你將如何處理「[我的(內容)在這裏)」? – leftclickben

+0

我用我試過的代碼編輯了我的問題 – user2027420

回答

0
$title = "[mycontent}outside(mycontent]outside{mycontent)"; 

$find = array(
    "/\{.*?\}/", 
    "/\{.*?\]/", 
    "/\{.*?\)/", 
    "/\[.*?\}/", 
    "/\[.*?\]/", 
    "/\[.*?\)/", 
    "/\(.*?\}/", 
    "/\(.*?\]/", 
    "/\(.*?\)/" 
); 
$replace = array(
    " ", 
    " ", 
    " ", 
    " ", 
    " ", 
    " ", 
    " ", 
    " ", 
    " " 
); 
$string = preg_replace($find, $replace, $title); 

var_dump($string); // prints string(17) " outside outside " 
1

您需要使用[]設置字符集並使用()保存字符集;

print preg_replace("~([\[\{\(]).*?([\]\}\)])~", "\\1...\\2", 
     "[mycontent} a (mycontent] b {mycontent)"); 

輸出:[...} a (...] b {...)