How can I get string between two characters [string] ? PHP

后端 未结 2 1483
鱼传尺愫
鱼传尺愫 2021-01-01 03:34
$string1 = \"This is test [example]\";
$string2 = \"This is test [example][2]\";
$string3 = \"This [is] test [example][3]\";

How can I get the foll

2条回答
  •  伪装坚强ぢ
    2021-01-01 04:23

    preg_match_all('/\[([^\]]+)\]/', $str, $matches);
    
    php > preg_match_all('/\[([^\]]+)\]/', 'This [is] test [example][3]', $matches);
    php > print_r($matches);
    Array
    (
        [0] => Array
            (
                [0] => [is]
                [1] => [example]
                [2] => [3]
            )
    
        [1] => Array
            (
                [0] => is
                [1] => example
                [2] => 3
            )
    
    )
    

    And here's the explanation for the rregex:

    \[ # literal [
    ( # group start
        [^\]]+ # one or more non-] characters
    ) # group end
    \] # literal ]
    

提交回复
热议问题