获取括号内的所有子字符串

发布于 2024-08-13 08:00:38 字数 217 浏览 9 评论 0原文

我想提取两个字符(括号)之间的所有字符串。

$string = "blah blah blah (blorp) blah blah (bloop) blah blah (bam)";

期望的输出:

['blorp', 'bloop', 'bam']

我不需要任何废话,只需要括号内的所有内容。

I want to extract all strings between two characters (parentheses).

$string = "blah blah blah (blorp) blah blah (bloop) blah blah (bam)";

Desired output:

['blorp', 'bloop', 'bam']

I don't need any of the blah blahs, just everything within parenthesis.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

终弃我 2024-08-20 08:00:38

您可以使用正则表达式来执行此操作:

$string = "blah blah blah (blorp) blah blah (bloop) blah blah (bam)";
preg_match_all("/\((.*?)\)/", $string, $result_array);

print_r( $result_array[1] ); // $result_array[0] contains the matches with the parens

这将输出:

Array
(
    [0] => blorp
    [1] => bloop
    [2] => bam
)

我的正则表达式使用非贪婪选择器: (.*?) 这意味着它将抓取尽可能“少”的内容。这可以防止它吃掉所有的 ) 并抓住开头 ( 和结尾 ) 之间的所有内容(无论多少个单词)。

You can do this with a regular expression:

$string = "blah blah blah (blorp) blah blah (bloop) blah blah (bam)";
preg_match_all("/\((.*?)\)/", $string, $result_array);

print_r( $result_array[1] ); // $result_array[0] contains the matches with the parens

This would output:

Array
(
    [0] => blorp
    [1] => bloop
    [2] => bam
)

My regular expression uses a non-greedy selector: (.*?) which means it will grab as "little" as possible. This keeps it from eating up all the ) and grabbing everything between the opening ( and the closing ) how ever many words away.

别把无礼当个性 2024-08-20 08:00:38

您可以将 preg_match_all 与类似的东西一起使用(只是一个快速草稿......):

preg_match_all("|\([^)]+\)|", $string, $result_array);

You could use preg_match_all with something like (just a quick draft...):

preg_match_all("|\([^)]+\)|", $string, $result_array);
夜司空 2024-08-20 08:00:38

全部使用正则表达式,这是一个没有正则表达式的

$string = "blah blah blah (blorp) blah blah (bloop) blah blah (bam)";
$s = explode(")",$string);
foreach ( $s as $k=>$v ){
    $m= strpos($v,"(" );
    if ($m){
        print substr( $v, $m+1  )  . "\n"  ;
    }
}

all using regex, here's one without regex

$string = "blah blah blah (blorp) blah blah (bloop) blah blah (bam)";
$s = explode(")",$string);
foreach ( $s as $k=>$v ){
    $m= strpos($v,"(" );
    if ($m){
        print substr( $v, $m+1  )  . "\n"  ;
    }
}
毅然前行 2024-08-20 08:00:38

如果您想要括号中的所有内容,那么您应该使用正则表达式。在你的情况下,这会做厚:

preg_match_all('/\(.*?\)/', $string, $matches);

If you want everything in parenthesis, then you should use regular expressions. In your case, this will do the thick:

preg_match_all('/\(.*?\)/', $string, $matches);
酒几许 2024-08-20 08:00:38

http://php.net/manual/en/function.preg-匹配所有.php

$matches = array();
$num_matched = preg_match_all('/\((.*)\)/U', $input, $matches);

http://php.net/manual/en/function.preg-match-all.php

$matches = array();
$num_matched = preg_match_all('/\((.*)\)/U', $input, $matches);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文