且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用正则表达式在方括号之间查找值

更新时间:2022-03-08 17:09:14

您可以使用以下正则表达式

[.*?][^]]*]

DEMO

您的代码将是

<?php
$str = "This is a test string. I want to get value between [this] and [/this]";
$regex = '~[.*?][^]]*]~';
if (preg_match($regex, $str, $m)) {
    $yourmatch = $m[0]; 
    echo $yourmatch;
    }
?> //=>  [this] and [/this]