【问题标题】:Remove Bracketed text from String using Regex & PHP使用正则表达式和 PHP 从字符串中删除括号内的文本
【发布时间】:2009-11-24 15:26:25
【问题描述】:

我正在尝试编写一个简单的 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 函数,但我无法让它工作,因为很明显我很不擅长正则表达式。

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

谢谢!

【问题讨论】:

    标签: php regex string


    【解决方案1】:

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

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

    【讨论】:

    • 如果输入符合上述标准,+1 是一个很好的解决方案。我写了一个更复杂的,允许(转义)方括号。但是,如果嵌套的非转义括号出现,那么正则表达式不是正确的方法。
    • Tim,您的解决方案非常棒,但由于我知道该序列永远不会创建除您所见之外的任何其他字符,因此这可能是所有必要的。谢谢萨尔曼!
    【解决方案2】:
    preg_replace("/\[.*?\]/", "", $mystring);
    

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

    【讨论】:

      【解决方案3】:
      preg_replace('~\[.*?\]~', '', $str);
      

      【讨论】:

        【解决方案4】:

        这里没有正则表达式,只是 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.
        

        【讨论】:

        • 我必须同意并不总是推荐正则表达式,因为它们可能变得不可读。但是对于这样一个简单的案例...... ;)
        • 不错的解决方案...但我必须同意...在这种情况下,正则表达式可能是最优雅的解决方案。
        • 优雅与否因人而异。如果它适合您,请继续使用正则表达式。 :)
        【解决方案5】:

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

        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\]]参加了杜兰大学建筑学院继续教育大会。她在题为“建筑环境从摇篮到摇篮的设计”的演讲中介绍了公司的工作。

        【讨论】:

          猜你喜欢
          • 2011-11-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-05-07
          • 1970-01-01
          • 1970-01-01
          • 2015-06-19
          相关资源
          最近更新 更多