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]

text 1複数行の場合、配列の最初の要素のすべてのコードではなく、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 に答える