2
[code]
 [1] text 1
 [2] text 2
 [3] text 3
[/code]

所以我需要这些值text 1, text 2, text 3是数组。

我在这方面的烦恼:

$matches = preg_match_all( "/^\[\d{1,3}\](.*)/", $content, $tags );

它返回给我一个空值的数组:

Array
(
    [0] => Array
        (
        )

    [1] => Array
        (
        )

)
1

如何修复它或使它变好?

[code]
 [1] text 1
 Different content here
 [2] text 2 Another dif..
 [3] Also something either
  to be continues!
[/code]

使用多行,它将仅返回 atext 1而不是数组第一个元素中的所有代码

Array
(
    [0] => Array
        (
            [0] => [1] text 1


            [1] => [2] text 2 Another dif..

            [2] => [3] Also something either

        )

    [1] => Array
        (
            [0] =>  text 1


            [1] =>  text 2 Another dif..

            [2] =>  Also something either

        )

)
1

其他元素也将仅由第一个字符串结果。

4

1 回答 1

4

您应该放下锚^并改为(.*)使用前瞻集([^[]*)

$s =<<<EOM
[code]
 [1] text 1
 Another line
 [2] text 2
 And another line too
 [3] text 3
[/code]
EOM;

preg_match_all("/\[\d{1,3}\]([^[]*)/", $s, $tags);

print_r($tags);
于 2012-09-28T04:54:27.090 回答