Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Regex和PHP从字符串中删除带括号的文本_Php_Regex_String - Fatal编程技术网

使用Regex和PHP从字符串中删除带括号的文本

使用Regex和PHP从字符串中删除带括号的文本,php,regex,string,Php,Regex,String,我正在尝试编写一个简单的php函数,它将删除字符串中两个括号之间的所有内容 以下是典型字符串的外观: 11月14日,凯瑟琳·格罗夫参加了杜兰大学建筑学院继续教育会议。她在题为“从摇篮到摇篮的建筑环境设计”的演讲中介绍了公司的工作。杜兰建筑学院继续教育会议旨在向杜兰建筑学院校友和当地建筑社区提供继续教育学分,重点关注可持续设计。Kathy目前负责该公司在新奥尔良的非营利组织Make It Right项目的无偿参与,包括评估在下九区建造LEED白金认证经济适用住房所用的所有材料。她负责该公司的住宅工

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

以下是典型字符串的外观:

11月14日,凯瑟琳·格罗夫参加了杜兰大学建筑学院继续教育会议。她在题为“从摇篮到摇篮的建筑环境设计”的演讲中介绍了公司的工作。杜兰建筑学院继续教育会议旨在向杜兰建筑学院校友和当地建筑社区提供继续教育学分,重点关注可持续设计。Kathy目前负责该公司在新奥尔良的非营利组织Make It Right项目的无偿参与,包括评估在下九区建造LEED白金认证经济适用住房所用的所有材料。她负责该公司的住宅工作室,包括领导北加州一个私人住宅项目的设计团队,该项目旨在获得LEED白金认证

基本上,我想从文本主体的开头去掉[img|u assist | nid=332 | title=| desc=| link=none | align=left | width=70 | height=61]

我已经有了一个使用preg_replace函数的php函数设置,但是我无法让它工作,因为显然我在正则表达式方面很差劲

您将编写什么正则表达式来选择上面的括号位(包括括号)

谢谢

preg_replace("/\[.*?\]/", "", $mystring);
匹配一对括号和括号之间可能的最小文本量。这是为了防止在有多对括号的情况下删除过多,即[a]bc[d]

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

匹配一对括号和括号之间可能的最小文本量。这是为了防止在有多对方括号(即[a]bc[d])的情况下删除过多。能否100%确保方括号内的字符串永远不会包含右方括号?如果是这样,那么这个简单的正则表达式就足够了:

preg_replace('~\[.*?\]~', '', $str);
preg_replace( "/\[[^\]]*\]/m", "", $string );

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

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

这里有一个没有正则表达式,只有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字符串函数

$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";
}

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

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|U assist | text=\[hello\]| nid=332 | title=| height=61]11月14日[test\\],凯瑟琳·格罗夫[hello\]]参加了杜兰大学建筑学院的继续教育会议。她在题为“建筑环境的从摇篮到摇篮的设计”的演讲中介绍了该公司的工作。

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

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|U assist | text=\[hello\]| nid=332 | title=| height=61]11月14日[test\\],凯瑟琳·格罗夫[hello\]]参加了杜兰大学建筑学院的继续教育会议。她在题为“建筑环境的从摇篮到摇篮的设计”的演讲中介绍了该公司的工作。

我不得不同意,并不总是推荐正则表达式,因为它们可能会变得不可读。但对于这样一个简单的案例;很好的解决方案…但我必须同意…在这种情况下,正则表达式可能是优雅的。优雅与否取决于每个人。如果它适合您,请继续使用正则表达式:我必须同意,并不总是推荐正则表达式,因为它们可能会变得不可读。但对于这样一个简单的案例;很好的解决方案…但我必须同意…在这种情况下,正则表达式可能是优雅的。优雅与否取决于每个人。如果它适合您,请继续使用正则表达式:+1如果输入符合上述标准,则为良好的解决方案。我已经写了一个更复杂的,允许转义方括号。但是,如果出现嵌套的未scaped括号,则正则表达式不是正确的方法。Tim,你的解决方案令人惊讶,但因为我知道序列将永远不会创建任何其他字符,而不是你看到的字符,这一个可能是所有必要的。谢谢你,萨尔曼+1如果输入符合上述标准,则为良好的解决方案。我已经写了一个更复杂的,允许转义方括号。但是,如果出现嵌套的未scaped括号,则正则表达式不是正确的方法。Tim,你的解决方案令人惊讶,但因为我知道序列将永远不会创建任何其他字符,而不是你看到的字符,这一个可能是所有必要的。谢谢你,萨尔曼!