Jump to content

Archived

This topic is now archived and is closed to further replies.

Boozi

preg_match_all with PREG_SET_ORDER ignoring potential match?

Recommended Posts

Hi all,

Long time reader, first time poster...

I've hit a wall trying to get this working : I'm trying to extract sets of fields from a non-static file. The fields inside this file are defined like this :
[code]

[[fieldname]]content of field.[[/fieldname]]

[/code]

Opening/closing tags & content can be spread across multiple lines like so:
[code]

[[field1]]content1[[/field1]]

[[field2]]
content2
[[/field2]]

[[field3]]
line1
line2
line3
[[/field3]]

[/code]

This is what I have so far:
[code]

preg_match_all( '/\[\[([a-z0-9]+)\]\](.*)\[\[\/([a-z0-9]+)\]\]/si' , $filestring , $fields , PREG_SET_ORDER );

[/code]

The resulting $fields contains this : (spaces added for extra readability)
[code]

    [0] => Array
        (
            [0] => [[field1]]content1[[/field1]]

                      [[field2]]
                      content2
                      [[/field2]]

                      [[field3]]
                      line1
                      line2
                      line3
                      [[/field3]]
            [1] => field1
            [2] => content1[[/field1]]

                      [[field2]]
                      content2
                      [[/field2]]

                      [[field3]]
                      line1
                      line2
                      line3

            [3] => field3
        )

[/code]

It seems that preg_match_all "skips" [[/field1]] and [[/field2]], am I overlooking something obvious?

The result I'm looking for is this : (spaces added for extra readability)
[code]

    [0] => Array
        (
            [0] => [[field1]]content1[[/field1]]
            [1] => field1
            [2] => content1
            [3] => field1
        )

    [1] => Array
        (
            [0] => [[field2]]
                      content2
                      [[/field2]]
            [1] => field2
            [2] => content2
            [3] => field2
        )

    [2] => Array
        (
            [0] => [[field3]]
                      line1
                      line2
                      line3
                      [[/field3]]
            [1] => field3
            [2] => line1
                      line2
                      line3
            [3] => field3
        )

[/code]

Any ideas or solutions would be much appreciated!

Share this post


Link to post
Share on other sites
[code]'/\[\[([a-z0-9]+)\]\](.*)\[\[\/([a-z0-9]+)\]\]/si'[/code]

In your first [tt]([a-z0-9]+)[/tt] you've told it to capture the field name, but in the second [tt]([a-z0-9]+)[/tt], you haven't told it to match the [b]same[/b] field name. Replace the second portion with [tt]\1[/tt].

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.