使用Regex& amp;删除String中的括号文本。 PHP

时间:2009-11-24 06:01:36

标签: php regex string

我正在尝试编写一个简单的php函数,它将删除字符串中两个括号之间的所有内容。

以下是典型字符串的样子:

[img_assist|nid=332|title=|desc=|link=none|align=left|width=70|height=61] On November 14, Katherine Grove participated in Tulane University's School of Architecture Continuing Education Conference . She presented the firm's work in a presentation titled "Cradle to Cradle Design for the Built Environment".The Tulane School of Architecture Continuing Education Conference is designed to make continuing education credits available to Tulane School of Architecture alumni and the local architecture community, with a focus on sustainable design. Kathy currently directs the firm’s pro-bono involvement with the nonprofit Make It Right project in New Orleans, including assessment of all materials used in the construction of LEED Platinum certified affordable homes in the Lower 9th Ward. She oversees the firm’s residential studio, including leading the design team for a private residential project in Northern California targeted for LEED Platinum certification

我基本上想要剥离你可以在主要开头看到的“[img_assist | nid = 332 | title = | desc = | link = none | align = left | width = 70 | height = 61]”文本正文。

我已经使用preg_replace函数设置了php函数,但是我无法让它工作,因为很明显我在使用正则表达式。

你会写什么正则表达式来选择上面的括号位(包括括号)?

谢谢!

5 个答案:

答案 0 :(得分:6)

您是否100%确定方括号内的字符串内部不会包含右方括号?如果是这样,那么这个简单的正则表达式就足够了:

preg_replace( "/\[[^\]]*\]/m", "", $string );

答案 1 :(得分:2)

preg_replace("/\[.*?\]/", "", $mystring);

匹配一对括号以及它们之间可能的最小文本量。 (这是为了防止在有多对括号时删除太多 - 即“[a] bc [d]”)。

答案 2 :(得分:2)

preg_replace('~\[.*?\]~', '', $str);

答案 3 :(得分:0)

这里没有正则表达式,只是PHP字符串函数

$str = <<<A
dfsdf[img_assist|nid=332|title=|desc=|link=none|align=left|width=70|height=61] 
On November 14, Katherine Grove participated in Tulane University's School of 
Architecture Continuing Education Conference [img_assist2|nid=332|title=] 
The Tulane School of Architecture Continuing Education  Conference is designed to make 
continuing education credits available to Tulane School of Architecture alumni and the local architecture community, with a focus on sustainable design.
    A;

$s = explode("]",$str);
foreach ($s as $a=>$b){
    if ( strpos($b ,"[")!==FALSE) {
        $z = explode("[",$b);
        $b=$z[0];
    }
    print $b."\n";
}

输出

$ php test.php
dfsdf
 On November 14, Katherine Grove participated in Tulane University's School of Architecture Continuing Education Conference
 The Tulane School of Architecture Continuing Education  Conference is designed to make continuing education credits available to Tulane School of Architecture alumni and the local architecture community, with a focus on sustainable design.

答案 4 :(得分:0)

一个带有正则表达式的解决方案,允许括号内的转义括号:

preg_replace("/\[(?:\\\\|\\\]|[^\]])*\]/", "", $mystring);

说明:

\[                # match an opening bracket [
    (?:           # match one of the following...
        \\\\      # first, if possible, match an escaped backslash \\
        |         # or, if unsuccessful...
        \\\]      # match an escaped right bracket \]
        |         # or, if unsuccessful...
        [^\]]     # match any character except a closing bracket ]
    )*            # zero or more times
\]                # finally, match a closing bracket ]

这将正确匹配

[img_assist|text=\[hello\]|nid=332|title=|height=61] 11月14日[test\\],Katherine Grove [hello\]]参加了杜兰大学建筑学院继续教育会议。她在题为“建筑环境从摇篮到摇篮设计”的演讲中介绍了该公司的工作。