我想知道如何使用 php 中的正则表达式删除一组括号和括号本身之间的文本,括号可能不同......例如:
[我的内容}
或者
(我的内容)
或者
{我的内容)
等等...
我努力了:
preg_replace("/\{.*\}/", " ", $title);
preg_replace("/\[.*\]/", " ", $title);
preg_replace("/\(.*\)/", " ", $title);
但这只是删除了相同的括号..是的,括号可以嵌套到二年级..
您需要使用设置字符集并使用;[]保存字符集()
print preg_replace("~([\[\{\(]).*?([\]\}\)])~", "\\1...\\2",
"[mycontent} a (mycontent] b {mycontent)");
输出:[...} a (...] b {...)
$title = "[mycontent}outside(mycontent]outside{mycontent)";
$find = array(
"/\{.*?\}/",
"/\{.*?\]/",
"/\{.*?\)/",
"/\[.*?\}/",
"/\[.*?\]/",
"/\[.*?\)/",
"/\(.*?\}/",
"/\(.*?\]/",
"/\(.*?\)/"
);
$replace = array(
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" "
);
$string = preg_replace($find, $replace, $title);
var_dump($string); // prints string(17) " outside outside "