2013-02-03 82 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 {...)